Interview Questions
- What is the difference between #include "filename" vs #include <filename>?
- What is the use of #define?
- Can a #define use a string character as replacement text?
- Can you give an example of #define?
- Can you give an example of #define with a Macro?
- 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.