Pre Incrementing Macro : C Preprocessor MCQ
This is typical question related to preprocessor directive. What will happen if we perform Pre incrementing macro operation.
Pre Incrementing Macro Question
Guess What will be the output of following code snippet when we perform pre incrementing macro operation -
#include<stdio.h> #define MAX 10 int main(){ int num; num = ++MAX; printf("%d",num); return 0; }
Options :
No | Options |
---|---|
A | 10 |
B | 11 |
C | Compile Error |
D | Run Time Error |
Output of the Above Program
Explanation :
num = ++MAX;
will be expanded as
num = ++10; = Compile Error
Macro Preprocessor only replaces occurance of macro symbol with macro symbol value so preprocessor will replace MAX with 10.
Expanded Program :
int main(){ int num; num = ++10; printf("%d",num); return 0; }
Above expanded code is now given to compiler. Compiler will throw error : Lvalue Require Error. (because Increment Operator and Decrement Operators should be used with variable)
Pre incrementing macro will results into error as compiler tries to increment the constant number.