C Preprocessor - C Programming MCQ Questions and Answers
Learn competitive and Technical Aptitude C programming mcq questions and answers on C Preprocessor with easy and logical explanations. Page-4 section-1
#include<stdio.h>
#define int char
void main()
{
int i = 65;
printf("sizeof(i)=%d", sizeof(i));
} Since the #define replaces the string int by the macro char.
So, here i is a variable of type char and not int.
#include<stdio.h>
#define square(x) x*x
void main()
{
int i;
i = 64/square(4);
printf("%d", i);
} The macro call square(4) will be substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority and associativity left to right, so the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64.
#include<stdio.h>
#define a 10
void main()
{
#define a 50
printf("%d", a);
} The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken.
I. The scope of a macro definition need not be the entire program.
II. The scope of a macro definition extends from the point of definition to the end of the file.
III. New line is a macro definition delimiter.
IV. A macro definition may go beyond a line.
#include<stdio.h>
#define clrscr() 100
void main()
{
clrscr();
printf("%dn", clrscr());
} Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler looks like this :
void main()
{
100;
printf("%d\n", 100);
}
Note: 100; is an executable statement but with no action. So it doesn't give any problem.
#include<stdio.h>
#define prod(a,b) a*b
void main()
{
int x=3,y=4;
printf("%d", prod(x+2,y-1));
} The macro expands and evaluates to as:
x+2*y-1 => x+(2*y)-1 => 10
#include<stdio.h>
gets replaced by the contents of the file stdio.h
#include<stdio.h>
#define max 5
void main(){
int i = 0;
i = max++;
printf("%d", i++);
} main.c:5:12: error: lvalue required as increment operand
i = max++;