in Programming in C edited by
426 views
0 votes
0 votes

What is the output of the following C program?

#include<stdio.h>
#define SQR(x) (x*x)
int main()
{
    int a;
    int b=4;
    
    a=SQR(b+2);
    printf("%d\n",a);
    return 0;
}
  1. 14
  2. 36
  3. 18
  4. 20
in Programming in C edited by
426 views

4 Comments

My answer is 36, it is correct or not?
0
0
14 is the answer because it will be evaluated as:-

b+2*b+2=4+2*4+2=4+8+2=14
2
2
Answer is 14 . # define is a macro definition . It replaces SQR(b+2) with (b+2*b+2).So a is computed as 14.

Note the placing of the brackets.
0
0
a=b+2*b+2

 =4+2*4+2

 =4+8+2 = 14...
1
1

1 Answer

2 votes
2 votes
As it is given (x*x) : The result will be b+2*b+2 which means: 4+2*4+2 = 14

Had it be ((x)*(x)): The answer would have been ((b+2)*(b+2)) = 36

Related questions

0 votes
0 votes
1 answer
1
kallu singh asked in Programming in C Dec 14, 2017
391 views
kallu singh asked in Programming in C Dec 14, 2017
391 views