Tuesday 23 April 2013

#define max 5;


What will be output if you will compile and execute the following c code?

#define max 5;
void main(){
int i=0;
i=max++;
printf("%d",i++);
}


(A) 5

(B) 6

(C) 7

(D) 0

(E) Compiler error


________________________________________
Explanation:
#define is token pasting preprocessor. If you will see intermediate file: test.i
test.c 1:
test.c 2: void main(){
test.c 3: int i=0;
test.c 4: i=5++;
test.c 5: printf("%d",i++);
test.c 6: }
test.c 7:
It is clear macro constant max has replaced by 5. It is illegal to increment the constant number. Hence compiler will show Lvalue required.

No comments:

Post a Comment