Macro definition to determine big endian or little endian machine?

Code supporting arbitrary byte orders, ready to be put into a file called order32.h: #ifndef ORDER32_H #define ORDER32_H #include <limits.h> #include <stdint.h> #if CHAR_BIT != 8 #error “unsupported char size” #endif enum { O32_LITTLE_ENDIAN = 0x03020100ul, O32_BIG_ENDIAN = 0x00010203ul, O32_PDP_ENDIAN = 0x01000302ul, /* DEC PDP-11 (aka ENDIAN_LITTLE_WORD) */ O32_HONEYWELL_ENDIAN = 0x02030001ul /* Honeywell 316 (aka … Read more

Inline functions vs Preprocessor macros

Preprocessor macros are just substitution patterns applied to your code. They can be used almost anywhere in your code because they are replaced with their expansions before any compilation starts. Inline functions are actual functions whose body is directly injected into their call site. They can only be used where a function call is appropriate. … Read more

Creating C macro with ## and __LINE__ (token concatenation with positioning macro)

The problem is that when you have a macro replacement, the preprocessor will only expand the macros recursively if neither the stringizing operator # nor the token-pasting operator ## are applied to it. So, you have to use some extra layers of indirection, you can use the token-pasting operator with a recursively expanded argument: #define … Read more

Real-world use of X-Macros

I discovered X-macros a couple of years ago when I started making use of function pointers in my code. I am an embedded programmer and I use state machines frequently. Often I would write code like this: /* declare an enumeration of state codes */ enum{ STATE0, STATE1, STATE2, … , STATEX, NUM_STATES}; /* declare … Read more

Overloading Macro on Number of Arguments

Simple as: #define GET_MACRO(_1,_2,_3,NAME,…) NAME #define FOO(…) GET_MACRO(__VA_ARGS__, FOO3, FOO2)(__VA_ARGS__) So if you have these macros, they expand as described: FOO(World, !) // expands to FOO2(World, !) FOO(foo,bar,baz) // expands to FOO3(foo,bar,baz) If you want a fourth one: #define GET_MACRO(_1,_2,_3,_4,NAME,…) NAME #define FOO(…) GET_MACRO(__VA_ARGS__, FOO4, FOO3, FOO2)(__VA_ARGS__) FOO(a,b,c,d) // expands to FOO4(a,b,c,d) Naturally, if you … Read more

Get all dates between 2 dates in vba

I am providing you the VBA code for this problem, with comments to help you understand the process. Please take time to read through and understand what is happening, so next time you run into a problem like this you have an understanding of where to start. If you have a go and get stuck, … Read more

Passing values to macros by for loop

It is normal! The preprocessor (the “thing” that processes the macros) is run BEFORE the C compiler. So, it is only valid when it produces compilable code. In your case, if you use the code you show j=Valve(1) it will work for that value, since it will produce: j=stTest.bValve1_Cmd but it will do the entire … Read more