-5
#define A 1+2
#define B 4+3

int main()
{
   int val = A*B;
   printf("%d",val);
}

Here A and B is defined, and val is (A*B). What is the correct output?

BlindCat
  • 295
  • 2
  • 8

2 Answers2

3

Let's trace the expansion and calculation manually.

A*B -> 1+2*4+3 -> 1+8+3 -> 12

As a result, the output will be 12

MikeCAT
  • 69,090
  • 10
  • 44
  • 65
2

This is a common issue with Macros. When you define something even as x+y in a macro it is advisable to first rap them in () so as to "protect" the operation. As such it would be better to define A as (1+2) etc. Otherwise you get the output 1+2*4+3=12 as people have stated above.

ameyCU
  • 16,146
  • 2
  • 24
  • 40
arduic
  • 639
  • 3
  • 12