I am trying to comprehend the output of my code over here:
#include <stdio.h>
#define DOUBLE(x) (x+x)
int main() {
int i, j;
i = j = 3;
printf("%d\n", DOUBLE(++i)); // Output: 10
printf("%d\n", DOUBLE(j++)); // Output: 7
return 0;
}
This is my interpreatation: DOUBLE
here is a macro, hence the pre-processor replaces
DOUBLE(++i)
to(++i + ++i)
and,DOUBLE(j++)
to(j++ + j++)
The compiler then pre-increments i
and post-increments j
before the addition operation in each statement.
- (5 + 4)
- (3 + 4)
My logic (fails) does not match the output. What is happening behind the scenes when I call DOUBLE
with argumnets having increment operators.