C Pre-Processor Macro code with () and {}

  • A+
#include <stdio.h> #define a (1,2,3) #define b {1,2,3}  int main() {     unsigned int c = a;     unsigned int d = b;     printf("%d/n",c);     printf("%d/n",d);     return 0; } 

Above C code will print output as 3 and 1.

But how are #define a (1,2,3) and #define b {1,2,3} taking a=3 and b=1 without build warning, and also how () and {} are giving different values?


Remember, pre-processor just replaces macros. So in your case you code will be converted to this:

#include <stdio.h>  int main() {     unsigned int c = (1,2,3);     unsigned int d = {1,2,3};     printf("%d/n",c);     printf("%d/n",d);     return 0; } 

In first case, you get result from , operator, so c will be equal to 3. But in 2nd case you get first member of initializer list for d, so you will get 1 as result.

2nd lines creates error if you compile code as c++. But it seems that you can compile this code in c.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: