907 views
5 votes
5 votes
#include <stdio.h>
#define A -B
#define B -C
#define C 5
int main(void)
{
printf("%d",A);
return 0;
}

now my doubt here is, before compilaion i think macro will make printf part as,                                                                    printf("%d",--5); which should give L-value error at compile time but,

actually what it is doing is printf("%d",-(-5)); leading to no compiler error and giving answer as 5, but macro implicitly never put precedence braces() itself...

1 Answer

Best answer
6 votes
6 votes

Let us make a simpler code version doing almost the same: (to avoid stdio.h which includes a lot of macros)

#define A -B
#define B -C
#define C 5
int main(void)
{
        int a = A;
        return 0;
}

Now, let us see the result after macro expansion

arjun@localhost:~$ gcc -E macro.c
# 1 "macro.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "macro.c"



int main(void)
{
 int a = - -5;
 return 0;
}

Hope that explains it -- after macro expansion, preprocessor inserts white space and thus prevent any addition of tokens being created.

selected by

Related questions

0 votes
0 votes
0 answers
1
samarpita asked Dec 9, 2021
316 views
Assembler,Linker,Macros are there in compiler design?
4 votes
4 votes
1 answer
2
ritwik_07 asked Jun 10, 2023
423 views
#include<stdio.h #include<string.h #define MAX(x, y) ((x) (y) ? (x) : (y)) int main() { int i = 10, j = 5, k = 0; k = MAX(i++, ++j); printf("%d %d %d", i, j, k); return ...