C Preprocessor(C Programming ) Questions and Answers

Question 1.

Will it result in to an error if a header file is included twice?


  1.    Yes
  2.    No
  3.    It is compiler dependent.
Explanation:-
Answer: Option C. -> It is compiler dependent.

Unless the header file has taken care to ensure that if already included it doesn't 

get included again.
Turbo C, GCC compilers would take care of these problems, generate no error.



Question 2.

Will the program compile successfully?
#include<stdio.h>

int main()

{

      #ifdef  NOTE

            int     a;

            a = 10;

      #else

           int a;

           a=20;

       #endif

        printf("%d`setminus`n" , a)'

        return 0;

}


  1.    Yes
  2.    No
Explanation:-
Answer: Option A. -> Yes

Yes, this program will compile and run successfully and prints 20.

The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the 

#ifdefblock statements. Here #ifdef condition fails because the Macro NOTE is nowhere declared.

Hence the #else block gets executed, the variable a is declared and assigned a value of 20.

printf("%dn", a); It prints the value of variable a 20.



Question 3.

It is necessary that a header files should have a .h extension?


  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

No, the header files have any kind of extension.



Question 4.

Would the following typedef work?
typedef #include l;


  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

Because typedef goes to work after preprocessing.



Question 5.


Will the program compile successfully?


#include<stdio.h>
#define X (4+Y)
#define Y (X+3)
int main()
{
printf("%d\n", 4*X+2);
return 0;
}
  1.    Yes
  2.    No
Explanation:-
Answer: Option B. -> No

Reports an error: Undefined symbol 'X'



Question 6.

Once preprocessing is over and the program is sent for the compilation the macros are removed

 from the expanded source code.


  1.    True
  2.    False
Explanation:-
Answer: Option A. -> True

True, After preprocessing all the macro in the program are removed.



Question 7.

A preprocessor directive is a message from compiler to a linker.


  1.    True
  2.    False
Explanation:-
Answer: Option B. -> False

FALSE

Example: #define symbol replacement

When the preprocessor encounters #define directive, it replaces any occurrence of symbol in 

the rest of the code by replacement. This replacement can be an statement or expression or a

 block or simple text.



Question 8.

The preprocessor can trap simple errors like missing declarations, nested comments or mismatch of braces.


  1.    True
  2.    False
Explanation:-
Answer: Option B. -> False

False, the preprocessor cannot trap the errors, it only replaces the macro with the given 

expression. But the compiler will detect errors.



Question 9.

A header file contains macros, structure declaration and function prototypes.


  1.    True
  2.    False
Explanation:-
Answer: Option A. -> True

True, the header file contains classes, function prototypes, structure declaration, macros.



Question 10.

In a macro call the control is passed to the macro.


  1.    True
  2.    False
Explanation:-
Answer: Option B. -> False

False, Always the macro is substituted by the given text/expression.