How copied macros are allowed in C?

If I want to use preprocessor instructions #defineto simplify the definition and calculation of constants and common functions, and use less RAM (as opposed to using values const). However, I'm not sure how they are resolved if many macros are used together.

I am developing my own code processing DateTime, similar to linux timestamps, but for playing with checkbox updates that are 1/60 of a second. I would rather declare the values ​​chained, but I wonder if more stringent encoding will be performed.

#include <stdint.h>

// my time type, measured in 1/60 of a second.
typedef int64_t DateTime;

// radix for pulling out display values
#define TICKS_PER_SEC  60L
#define SEC_PER_MIN    60L  
#define MIN_PER_HR     60L
#define HRS_PER_DAY    24L
#define DAYS_PER_WEEK   7L
#define WEEKS_PER_YEAR 52L

// defined using previous definitions (I like his style, write once!)
#define TICKS_PER_MIN    TICKS_PER_SEC * SEC_PER_MIN
#define TICKS_PER_HR     TICKS_PER_SEC * SEC_PER_MIN * MIN_PER_HR
#define TICKS_PER_DAY    TICKS_PER_SEC * SEC_PER_MIN * MIN_PER_HR * HRS_PER_DAY
// ... so on, up to years

//hard coded conversion factors.
#define TICKS_PER_MIN_H    3600L      // 60 seconds = 60^2 ticks
#define TICKS_PER_HR_H     216000L    // 60 minutes = 60^3 ticks
#define TICKS_PER_DAY_H    5184000L   // 24 hours   = 60^3 * 24 ticks

// an example macro to get the number of the day of the week
#define sec(t)((t / TICKS_PER_DAY) % DAYS_PER_WEEK)

sec(t), TICKS_PER_DAY, TICKS_PER_SEC * SEC_PER_MIN * MIN_PER_HR * HRS_PER_DAY, , sec(t):

(t / 5184000L) % 7L)

:

(t / (60L * 60L * 60L * 24L)) % 7L)

? , , ?

UPDATE:

, , ,

1. :

(t / 60 * 60 * 60 * 24) != (t / (60 * 60 * 60 * 24))

2. , :

// note parentheses to prevent out-of-order operations
#define TICKS_PER_MIN    (TICKS_PER_SEC * SEC_PER_MIN)
#define TICKS_PER_HR     (TICKS_PER_SEC * SEC_PER_MIN * MIN_PER_HR)
#define TICKS_PER_DAY    (TICKS_PER_SEC * SEC_PER_MIN * MIN_PER_HR * HRS_PER_DAY)
+4
4

. gcc preprocessor macro docs, - .

, . , ,

(t / (60L * 60L * 60L * 24L)) % 7L)

, ( ?)

(t / 5184000L) % 7L)

, , / .

1: "(t)" /. 2: - undef, . . , ( ).

UPDATE: - :

, , . ,

#define TABLESIZE BUFSIZE #define BUFSIZE 1024 TABLESIZE ==> BUFSIZE ==> 1024 TABLESIZE BUFSIZE, , , 1024.

, BUFSIZE TABLESIZE. "#define TABLESIZE - BUFSIZE - , . TABLESIZE .

( )

+1

. "" . , , .

, , " ", , . , t , 30 * 20 * t 30 * t * 20.

+2

- - , . , , , .

, C. , C , , . TICKS_PER_DAY:

#define TICKS_PER_DAY    TICKS_PER_SEC * SEC_PER_MIN * MIN_PER_HR * HRS_PER_DAY

sec ( , , ):

#define sec(t)((t / TICKS_PER_DAY) % DAYS_PER_WEEK);

sec(x), :

((x / 60L * 60L * 60L * 24L) % 7L);

, . 60L, .

- TICKS_PER_DAY, :

#define TICKS_PER_DAY    (TICKS_PER_SEC * SEC_PER_MIN * MIN_PER_HR * HRS_PER_DAY)

, , sec , , , sec(x) + 10:

#define sec(t)  ((t / TICKS_PER_DAY) % DAYS_PER_WEEK)

, sec(x) :

((x / (60L * 60L * 60L * 24L)) % 7L)

, . , , .

: , . . ​​ .

+1

:

(t / (60L * 60L * 60L * 24L)) % 7L)

, , ( ).

BUT this does not mean that all calculations will be repeated at every point at which you use sec (t). This is because the calculation happens at compile time. This way you do not pay the price at runtime. The compiler pre-computes such constant calculations and uses the calculated value in the generated code.

+1
source

Source: https://habr.com/ru/post/1620183/


All Articles