Moh'd H Moh'd H - 1 year ago 86
C Question

not understanding how the output is generated

i expect the output (or value of silly) to be

. but what i get is
. this is fixed when i add parentheses:
#define THING (4+2)

but i still want to know what is happening when there are no parentheses and why im getting an output of

the following is my code:

#include <stdio.h>
#include <stdlib.h>

#define THING 4+2

int main(void)

int silly = THING * THING;
printf("%d", silly);


Answer Source
THING*THING = 4+2*4+2 = 4+(2*4)+2 // because of higher precedence of * than +
            = 4+8+2 = 14.

Do remember that MACROs are exactly replaced(substituted) into the code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download