Redirected
recategorized by
12,763 views
10 10 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

9 Answers

Best answer
18 18 votes

The macro function SQR(x)(x*x) calculate the square of the given number 'x'. (Eg: 102)

Step 1int a, b=4; Here the variable a, b are declared as an integer type and the variable b is initialized to 4.

Step 2a = SQR(b+2); becomes,

=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .

=> a = 4+2 * 4+2;

=> a = 4 + 8 + 2;

=>a=14
selected by
12 12 votes

Ans will be 14

#include <stdio.h>
#define SQR(x) (x*x)
 
int main(void) {
	int a;
	int b=4;
	a=SQR(b+2); // here it will compute (b+2*b+2)=4+2*4+2=4+8+2=14
	printf("%d",a);
	return 0;
}
3 3 votes
It will print 14.

Because due to macro expression will get converted into

b + 2 * b + 2 === > 4 + 2 * 4 + 2 ==> 14
3 3 votes
preprocessor expands, a = SQR(b+2); as

a= x + 2 * x + 2

a= 4 + 8 + 2

a = 14

option A would be the answer
1 1 vote
Consider the given program
#include<stdio.h>
int main()
{
int a;
int b=4;
a=SQR(b+2);//a=b+2*b+2
printf("%d\n",a);
return 0;
}

Here SQR(x) is replaced by macro to x*x

a=SQR (b+2)

  =b+2 * b+2

   = 4+2 * 4+2

   =4+8+2

So the program assign a=4+8+2=14 to variable a.14 will be printed .

edited by
Answer:
Position:
Show:

Related questions

3 3 votes
4 answers 4 answers
7.3k
7.3k views
jenny101 asked Jun 25, 2016
7,292 views
The following three 'C' language statements is equivalent to which single statement?y=y+1; z=x+y; x=x+1z = x + y + 2;z = (x++) + (++y);z = (x++) + (y++);z = (x++) + (++y)...
8 8 votes
3 answers 3 answers
10.4k
10.4k views
Sourabh Kumar asked Jun 22, 2016
10,381 views
How many lines of output does the following C code produce?#include<stdio.h float i=2.0; float j=1.0; float sum = 0.0; main() { while (i/j 0.001) { j+=j; sum=sum+(i/j); ...
9 9 votes
8 answers 8 answers
14.3k
14.3k views
ajit asked Sep 2, 2015
14,348 views
What is the output of the following C program?#include<stdio.h void main(void){ int shifty; shifty=0570; shifty=shifty>>4; shifty=shifty<<6; printf("The value of shifty i...
11 11 votes
2 2 answers
229
229 views
GO Classes asked Jun 8
229 views
What is the output of the following code?#include <stdio.h #define SQUARE(x) x * x int main() { int a = 3; int result = SQUARE(a + 1); printf("%d", result); return 0; }$\...