vendredi 7 juin 2019

how macro works different with integers and char in my C-code

I'am familiar with the conditional compilation with macros in "C" language but some piece of code makes me confused ,I can understand how CODE-1 works, nothing is assigned into X therefore Y is defined as 5 (else condition) and on print Y we will get 5 as a output.

but in the CODE-2 which is quite similar to the CODE-1 except the "#if X == A" condition which gives the output as 3,which i dont understand how it can produce output as 3 .Can anyone tells me how "#if X == 3" and "#if X == A" makes the difference in the output.

CODE-1

#include <stdio.h>
#if X == 3
#define Y 3
#else
#define Y 5
#endif

int main()
{
printf("%d", Y);
return 0;
}

//output : 5

CODE-2

#include <stdio.h>
#if X == A
#define Y 3
#else
#define Y 5
#endif

int main()
{
printf("%d", Y);
return 0;
}

//output : 3

“I expect the output of CODE-2 to be 5, but the actual output is 3.”

Aucun commentaire:

Enregistrer un commentaire