11.
In which stage the following code
#include<stdio.h>
gets replaced by the contents of the file stdio.h
A. During editing
B. During linking
C. During execution
D. During preprocessing
Answer: Option D
Explanation:
The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More precisely, the entire text of the file ‘stdio.h’ replaces the #include directive.
12.
What will be the output?
#include<stdio.h>
#define SQR(x)(x*x)
int main()
{
int a, b=3;
a = SQR(b+2);
printf(“%d\n”, a);
return 0;
}
A. 25
B. 11
C. Error
D. Garbage value
Answer: Option B
Explanation:
The macro function SQR(x)(x*x) calculate the square of the given number ‘x’. (Eg: 102)
Step 1: int a, b=3; Here the variable a, b are declared as an integer type and the variable b is initialized to 3.
Step 2: a = SQR(b+2); becomes,
=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .
=> a = 3+2 * 3+2;
=> a = 3 + 6 + 2;
=> a = 11;
Step 3: printf(“%d\n”, a); It prints the value of variable ‘a’.
Hence the output of the program is 11
13.
What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?
#include<stdio.h>
#define SWAP(a, b, c) (c t; t=a, a=b, b=t)
int main()
{
int x=10, y=20;
SWAP(x, y, int);
printf(“%d %d\n”, x, y);
return 0;
}
A. It compiles
B. Compiles with an warning
C. Not compile
D. Compiles and print nothing
Answer: Option C
Explanation:
The code won’t compile since declaration of t cannot occur within parenthesis.
14.
What will be the output of the program?
#include<stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);
int main()
{
int i=10, j=5, k=0;
k = MAN(++i, j++);
printf(“%d, %d, %d\n”, i, j, k);
return 0;
}
A. 12, 6, 12
B. 11, 5, 11
C. 11, 5, Garbage
D. 12, 6, Garbage
Answer: Option A
Explanation:
The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.
Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.
Step 2: k = MAN(++i, j++); becomes,
=> k = ((++i)>(j++)) ? (++i):(j++);
=> k = ((11)>(5)) ? (12):(6);
=> k = 12
Step 3: printf(“%d, %d, %d\n”, i, j, k); It prints the variable i, j, k.
In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.
Hence the output of the program is 12, 6, 12
15.
What will be the output of the program?
#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
float s=10, u=30, t=2, a;
a = 2*(s-u*t)/SQUARE(t);
printf(“Result = %f”, a);
return 0;
}
A. Result = -100.000000
B. Result = -25.000000
C. Result = 0.000000
D. Result = 100.000000
Answer: Option A
Explanation:
The macro function SQUARE(x) x*x calculate the square of the given number ‘x’. (Eg: 102)
Step 1: float s=10, u=30, t=2, a; Here the variable s, u, t, a are declared as an floating point type and the variable s, u, t are initialized to 10, 30, 2.
Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,
=> a = 2 * (10 – 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .
=> a = 2 * (10 – 30 * 2) / 2 * 2;
=> a = 2 * (10 – 60) / 2 * 2;
=> a = 2 * (-50) / 2 * 2 ;
=> a = 2 * (-25) * 2 ;
=> a = (-50) * 2 ;
=> a = -100;
Step 3: printf(“Result=%f”, a); It prints the value of variable ‘a’.
Hence the output of the program is -100