Why sizeof(MACRO) gives an output of 4 bytes as MACRO does not hold any memory location?

  • A+
Category:Languages

Below is the small program:

#include <stdio.h> #define max 'A'  int main() {     char a;     printf("max[%d] sizeof[max][%ld]/n", max, sizeof(max));     a = max;     printf("a[%d] sizeof[a][%ld]/n", a, sizeof(a));     return 0; } 

And output of the program is:

max[65] sizeof[max][4] a[65] sizeof[a][1] 

Can anyone help me to understand why the sizeof(max) is 4 bytes?


sizeof(max) is replaced by the preprocessor with sizeof('A'). sizeof('A') is the same as sizeof(int), and the latter is 4 on your platform.

For the avoidance of doubt, 'A' is an int constant in C, not a char. (Note that in C++ 'A' is a char literal, and sizeof(char) is fixed at 1 by the standard.)

Comment

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