Search code examples
clogic

in c how preprocessor(#) get evaluated ? not understanding the output of this program


what would be the output of this program ?

#include <stdio.h>
#define s(x)x*x
int main()
{ printf("%d",s(5+1));

      return 0;
}

the output of this code is 11 how?


Solution

  • It is because 5 + 1 * 5 + 1 is 11. The multiplication comes first, 1 * 5, so you're left with 5 + 5 + 1 => 11.

    To get 36, change your macro to:

    #define s(x) ((x)*(x))
    

    Which will then become ((5 + 1) * (5 + 1)) => (6 * 6) => (36)