C Preprocessors Set 1

Ques: 1 What will be the output of the following program?
#include<stdio.h>
#define CUBE(x) x*x*x
int main()
{
printf("%d\n",CUBE(1+2));
return 0;
}
A. 9
B. 7
C. Error
D. Compiler dependent
Description
Answer: B
CUBE(1+2) expands as 1+2*1+2*1+2 .
Now since * operators has higher priority than addition operator calculation goes on
like 1+2+2+2 =7.
This is one problem with macro that could be solved using proper parenthesis.
CUBE(x) (x)*(x)*(x) would have given the expected result.


Ques: 2 What will be the output of the following program?
#include<stdio.h>
#define PROD(x,y) (x)*(y)
int main()
{
    printf("%d\n",36/PROD(2,3));
    return 0;
}
A. 6
B. error
C. 24
D. 54
Description
Answer: D
Again an unexpected result. The reason is same.
36/PROD(2,3)expands as 36/2*3 ,again / has higher priority and hence it results into 18*3=54.
This concludes that if your macro accepts parameters, it is better to put parenthesis
around entire macro expression and also around each argument.

Ques: 3 What will be the output of the following program?
#include<stdio.h>
int main()
{
    #define a  4 
    if (a << 1)
     printf("%d\n", a);
}
A. 8
B. 2
C. Error:Misplaced directive
D. 4
Description
Answer: D
Processor directives can be placed anywhere in a program.
if condition evaluates to 8 but does not change value of a which is true and a is printed.

Note: Shifting n bytes of number towards lefty is equivalent to multiplying the number by 2n.


Ques: 4 What will be the output of the following program?
#include<stdio.h>
#
int main()
{
    #define a 4 
    if (a << 1)
     printf("%d\n", a);
}
A. Error:no macro name given in # directive
B. 8
C. 4
D. Misplaced Macro a
Description
Answer: C
The code works fine and prints a=4.
Preprocessor directive only of the symbol # is null directive and it has no effect.

Ques: 5 What will be the output of the following program?
#include<stdio.h>
#define b 3;
int main()
{
    #define a (4) 
    printf("%d\n", a*b);
}
A. 4
B. 3
C. 12
D. Error
Description
Answer: D
Error #define b 3 ; 
Program shows error because macro definition does not end with semicolon.

Ques: 6 What will be the output of the following program?
#include<stdio.h>
#define FORMAT %d%d
void main()
{
    int a = 6, b = -8;
    a=a<<2;
    b=b>>2;
    printf("\nFORMAT ", a, b);
}
A. 24 -2
B. 24 -9
C. FORMAT
D. FORMAT GARBAGE GARBAGE
Description
Answer: C
Program will print FORMAT and prints nothing for a and b.
If the macroname appears inside a character constant or string constant or a 
comment it is not replaced and is left as it is.

Ques: 7 What will be the output of the following program?
#include<stdio.h>
#define TEST
#define a 4
#define b 3
int main()
{
    printf("%d %d",a,b);
    TEST;
    printf(" Error: no macro definition");

}
A. Program prints 4 3 TEST Error: no macro definition
B. COMPILE TIME ERROR: No macro definition
C. COMPILE TIME ERROR: Unidentified identifier TEST
D. Program prints 4 3 Error: no macro definition
Description
Answer: D
A macro without  macro expansion works with no error. 
Hence the code prints 3 and 4,does nothing for TEST and  
then executes the next printf statement.

Ques: 8 What will be the output of the following program?
#include<stdio.h>
#define SWAP(dtype,x,y) {dtype t; t=x;x=y;y=t;}
int main(void)
{
    int a=2,b=5;
    SWAP(int,a,b)
    printf("a=%d b=%d\n",a,b);
    return 0;
}
A. a=2 b=5
B. a=5 b=2
C. Compile time error
D. Run time error
Description
Answer: B
The program swaps the value.
 Macro SWAP(int,a,b) is expanded to { int t; t=a;a=b;b=t;}.
 

Ques: 9 What will be the output of the following program?
#include<stdio.h>
#define SWAP(dtype,x,y) {dtype t; t=x;x=y;y=t;}
int main(void)
{
    int s=2,t=5;
    SWAP(int,s,t)
    printf("s=%d t=%d\n",s,t);
    return 0;
}
A. s=2 t=5
B. s=5 t=2
C. Compile time error:Redeclaration of variable t
D. Run time error
Description
Answer: A
Macro SWAP(int,a,b) is expanded to { int t; t=s;s=t;t=t;}.
The values here are not swapped.
When the macro is expanded there is conflict between variable t declared 
inside main and variable t declared outside main().

Ques: 10 What will be the output of the following program?
S: nesting of macros is allowed
T: macro is active from the point of its appearance till the end of program
U: getchar() and putchar()  are defined as macro in stdio.h
V: #undef is used to remove a macro
A. STV
B. STU
C. STUV
D. TUV
Description
Answer: C
   
Macros can be nested.One macro name can also be used for defining another macro name 
i.e macro expansion can also contain name of another macro.

When #undef macro_name is encountered then macro will not be replaced by its expansion.


Ques: 11 What will be the output of the following program?
#include<stdio.h>
#define PASTE(i, j) i##j
int main()
{
    int k1=10;
    int k12=20;
    printf("%d\n", PASTE(k1, 2));
    return 0;
}
A. 10
B. 20
C. 30
D. Error
Description
Answer: B
## is token pasting operator used in the macro expansion to concatentenate two tokens into a single token.

Ques: 12 What will be the output of the following program?
#include<stdio.h>
#define SHOW(variable) printf(#variable"=%d",variable);
int main()
{
    int x=3;
    SHOW(x);
    return 0;
}

A. variable=3
B. #variable=3
C. Error: unknown identifier # in macro definition.
D. x=3
Description
Answer: D
In definition of macro,  if a formal argument occurs inside a string in the macro expansion
then it is not replaced by actual argument.To solve this problem Stringizing Operator # is used.
It replaces formal parameter in string by actual parameter.
For eg: #define SHOW(variable) printf("var=%d",variable)
A call SHOW(x) in main() with x=3 would printf var=3.Var in string doesn't get replaced by x.
In the question using # operator variable is replaced by x.

Ques: 13 What will be the output of the following program?
#include<stdio.h>
#define VOID 20
int main()
{
    int a = 20;
    #if VOID == a
    printf("GEEK ");
    #endif
    #if VOID == 20
    printf("BIX\n");
    #endif
}
A. Error: Void cannot be used
B. GEEK
C. BIX
D. Compile time error:Unknown identifier #if #endif
Description
Answer: C
An expression which is followed by #if is evaluated if result is nonzero. If result is non zero then the statements between #if and #endif are compiled ,otherwise they are skipped.
Also in the question #if VOID ==a here preprocessor searches for  macro a(not variable a), which is not defined hence condition is false.

Note: Expression in #if must be an integral expression only , enum constants,size of operator,cast operator or any other variables or keywords are not allowed



comments powered by Disqus