#include <stdio.h> #define abs(a) (a) < 0 ? -(a) : (a) int main(void) { printf("%d\n", abs(-3) + 1); return 0; } this code snippet, herbert schildt's book, looks produce output 4 prints 3. why?
how fix it?
expand macro:
#define abs(a) (a) < 0 ? -(a) : (a) printf("%d\n", abs(-3) + 1); printf("%d\n", (-3) < 0 ? -(-3) : (-3) + 1); // can **gcc -e source.c printf("%d\n", (-3) < 0 ? 3 : -2); //read precedence understand step. printf("%d\n", 3); this step step explanation of why printing 3. need fix using appropriate parenthesis.
Comments
Post a Comment