C Interview Questions on Preprocessor Directives

We discuss various C Language interview questions about Preprocessor Directives.

Interview Questions

  1. What is the difference between #include "filename" vs #include <filename>?
  2. What is the use of #define?
  3. Can a #define use a string character as replacement text?
  4. Can you give an example of #define?
  5. Can you give an example of #define with a Macro?
  6. What is the output of the #define example program below?

What is the difference between #include "filename" vs #include <filename>?

If the filename is quoted, the directory where the source program was found is searched first; Then search follow the implementation defined rule.

If the filename is enclosed in < and >, implementation-defined rule to used to find the file.

What is the use of #define?

A #define line defines a symbolic name or symbolic constant to be a particular string of characters.

Can a #define use a string character as replacement text?

The replacement text can be any sequence of characters; it is not limited to numbers.

Can you give an example of #define?

#include <stdio.h>
#include <stdlib.h>

#define NUMBER_OF_SUBJECTS 5

int main() {

	int marks[] = {90,91,93,94,99};

	for(int i=0;i<NUMBER_OF_SUBJECTS;i++)
		printf("subject:%d marks:%d\n",i,marks[i]);
}

Can you give an example of #define with a Macro?

Example program below:

#include <stdio.h>
#include <stdlib.h>

#define MAX(x,y) x>y?x:y

int main() {
		printf("max:%d",MAX(5,8));//max:8
}

What is the output of the #define example program below?

#include <stdio.h>

#define ABC  10

void main()
{
       printf("%d\n", ABC);//10

       #define ABC  20

       printf("%d\n", ABC);//20

}

#define value can be redefined anywhere in the program. Output of the program is shown in the comment.

FREE JAVA INTERVIEW GUIDE

ONE SUBSCRIBER EVERY MONTH WINS 100% DISCOUNT CODE FOR INTERVIEW GUIDE WITH 23 VIDEOS

We Respect Your Privacy