C Preprocessor Test 1


1) In which stage the below code gets replaced by the contents of the file #include<stdio.h>

  1. During linking
  2. During editing
  3. During preprocessing
  4. During execution

The correct option is (c).

Explanation:

During preprocessing stage the line #include<stdio.h> with the system header file of that name gets replaced by the contents of file stdio.h.

Therefore the entire text of the file 'stdio.h' replaces with #include directive.

2) C preprocessor directive #undef can be used with a macro that has been #define earlier.

  1. True
  2. False

The correct option is (a).

Explanation:

True, the directive #undef can be used only with a macro that has been #define earlier in a program.

For Example: #define PI 3.14

We can undefine PI macro by #undef PI

3) C preprocessor directive #ifdef...#elif?#endif is used for conditional compilation.

  1. True
  2. False

The correct option is (a).

Explanation:

True, the C macros like #ifdef...#elif?#endif are used for performing conditional operation in C program.

The syntax of C preprocessor directive is:

4) What will be the output of the below program?

  1. x=10 , y=20
  2. x=20, y=10
  3. Error: Undefined symbol 't'
  4. Error: Declaration not allowed in macro

The correct option is (b).

Explanation:

The macro statement SWAP(x, y) int t; t=x, x=y, y=t; swaps the value of given two variable.

Step 1: int x=10, y=20; The variable x and y are declared as an integer type and initialized to 10, 20 respectively.

Step 2: SWAP(x, y);. Here the macro is substituted and it swaps the value to variable x and y.

Hence the output of the program is x=20, y=10.

5) Which of the following are correctly formed #define statements in C language?

  1. #define CUBE(x) (X*X*X)
  2. #define CUBE(X) {X*X*X}
  3. #define CUBE (X) X*X*X
  4. #define CUBE(X) (X)*(X)*(X)

The correct option is (d).

Explanation:

The syntax for macro definition with argument is:

  • There should be no space between macro's name and it's '(args)'.
  • The variables used as macro's argument are case sensitive and its expansion should be same. i.e. 'x' and 'X' are different variables.
  • A macro expansion should be enclosed within parenthesis '( )' (do not use { } or [ ]).




Latest Courses