RejinpaulPlacement
Search

C Preprocessor - C Programming Questions and Answers - RejinpaulPlacement

@ : Home > C Programming > C Preprocessor


Find us on Facebook



Follow us on Google+


Google+

1.
In which stage the following code
#include
gets replaced by the contents of the file stdio.h
Answer & Solution
Answer: (D) During preprocessing
Solution: The preprocessor replaces the line #include with the system header file of that name. More precisely, the entire text of the file 'stdio.h' replaces the #include directive.
close
2.
What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?

#include
#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;
}
Answer & Solution
Answer: (C) Not compile
Solution: The code won't compile since declaration of t cannot occur within parenthesis.
close
3.
What will be the output of the program?

#include
#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)

int main()
{
int x;
x = MAX(3+2, 2+7, 3+7);
printf("%d\n", x);
return 0;
}
Answer & Solution
Answer: (C) 10
Solution: The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.

Step 1: int x; The variable x is declared as an integer type.
Step 2: x = MAX(3+2, 2+7, 3+7); becomes,
=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)
=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )
=> x = (5 >9 ? (10): (10) )
=> x = 10
Step 3: printf("%d\n", x); It prints the value of 'x'.
Hence the output of the program is "10".
close
4.
What will be the output of the program?

#include
#define PRINT(i) printf("%d,",i)

int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0
}
Answer & Solution
Answer: (A) 2, 3, 4,
Solution: The macro PRINT(i) print("%d,", i); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.

Step 2: PRINT(x); becomes printf("%d,",x). Hence it prints '2'.

Step 3: PRINT(y); becomes printf("%d,",y). Hence it prints '3'.

Step 4: PRINT(z); becomes printf("%d,",z). Hence it prints '4'.

Hence the output of the program is 2, 3, 4.
close
5.
What will be the output of the program?

#include
#define MESS junk

int main()
{
printf("MESS\n");
return 0;
}
Answer & Solution
Answer: (B) MESS
Solution: printf("MESS\n"); It prints the text "MESS". There is no macro calling inside the printf statement occured.
close