Geeksquiz | Macro & Preprocessor

macro & preprocessor

-----------

Macro & Preprocessor

Question 1
WRONG
#include <stdio.h>
#define PRINT(i, limit) do \
                         { \
                             if (i++ < limit) \
                             { \
                                 printf ( "GeeksQuiz\n" ); \
                                 continue ; \
                             } \
                         } while (1)
 
int main()
{
     PRINT(0, 3);
     return 0;
}
How many times  GeeksQuiz is printed in the above program?
A
1
3
C
4
Compile-time error

Discuss it


Question 1 Explanation: 
The  PRINT macro gets expanded at the pre-processor time i.e. before the compilation time. After the macro expansion, the if expression becomes:  if (0++ < 3). Since  0 is a constant figure and represents only r-value, applying increment operator gives compile-time error: lvalue required. lvalue means a memory location with some address.
Question 2
CORRECT
#include <stdio.h>
#if X == 3
     #define Y 3
#else
     #define Y 5
#endif
 
int main()
{
     printf ( "%d" , Y);
     return 0;
}
What is the output of the above program?
A
3
5
C
3 or 5 depending on value of X
D
Compile time error

Discuss it


Question 2 Explanation: 
In the first look, the output seems to be compile-time error because macro X has not been defined. In C, if a macro is not defined, the pre-processor assigns 0 to it by default. Hence, the control goes to the conditional else part and 5 is printed. See the next question for better understanding.
Question 3
CORRECT
What is the output of following program?
#include <stdio.h>
#define macro(n, a, i, m) m##a##i##n
#define MAIN macro(n, a, i, m)
 
int MAIN()
{
     printf ( " GeeksQuiz " );
}
A
Compiler Error
GeeksQuiz
C
MAIN
D
main

Discuss it


Question 3 Explanation: 
The program has a preprocessor that replaces "MAIN" with "macro(n, a, i, m)". The line "macro(n, a, i, m)" is again replaced by main. The key thing to note is  token pasting operator ## which concatenates parameters to macro.
Question 4
WRONG
#include <stdio.h>
#define X 3
#if !X
     printf ( "Geeks" );
#else
     printf ( "Quiz" );
  
#endif
int main()
{
         return 0;
}
Geeks
B
Quiz
Compiler Error
D
Runtime Error

Discuss it


Question 4 Explanation: 
A program is converted to executable using following steps 1) Preprocessing 2) C code to object code conversion 3) Linking The first step processes macros. So the code is converted to following after the preprocessing step.
printf("Quiz");
int main()
{
        return 0;
}
The above code produces error because printf() is called outside main. The following program works fine and prints "Quiz"
#include 
#define X 3

int main()
{
#if !X
    printf("Geeks");
#else
    printf("Quiz");

#endif
    return 0;
}
Question 5
CORRECT
#include <stdio.h>
#define ISEQUAL(X, Y) X == Y
int main()
{
     #if ISEQUAL(X, 0)
         printf ( "Geeks" );
     #else
         printf ( "Quiz" );
     #endif
     return 0;
}
Output of the above program?
Geeks
B
Quiz
C
Any of Geeks or Quiz
D
Compile time error

Discuss it


Question 5 Explanation: 
The conditional macro  #if ISEQUAL(X, 0) is expanded to  #if X == 0. After the pre-processing is over, all the undefined macros are initialized with default value 0. Since macro X has not been defined, it is initialized with 0. So,  Geeks is printed.
Question 6
WRONG
#include <stdio.h>
#define square(x) x*x
int main()
{
   int x;
   x = 36/square(6);
   printf ( "%d" , x);
   return 0;
}
1
36
C
0
D
Compiler Error

Discuss it


Question 6 Explanation: 
Preprocessor replaces square(6) by 6*6 and the expression becomes x = 36/6*6 and value of x is calculated as 36. Note that the macro will also fail for expressions "x = square(6-2)" If we want correct behavior from macro square(x), we should declare the macro as
#define square(x) ((x)*(x))  
Question 7
WRONG
Output?
# include <stdio.h>
# define scanf  "%s Geeks Quiz "
int main()
{
    printf ( scanf , scanf );
    return 0;
}
Compiler Error
B
%s Geeks Quiz
C
Geeks Quiz
%s Geeks Quiz Geeks Quiz

Discuss it


Question 7 Explanation: 
After pre-processing phase of compilation, printf statement will become. printf("%s Geeks Quiz ", "%s Geeks Quiz "); Now you can easily guess why output is "%s Geeks Quiz Geeks Quiz".
Question 8
WRONG
#include <stdio.h>
#define a 10
int main()
{
   printf ( "%d " ,a);
 
   #define a 50
 
   printf ( "%d " ,a);
   return 0;
}
Compiler Error
10 50
C
50 50
D
10 10

Discuss it


Question 8 Explanation: 
Preprocessor doesn't give any error if we redefine a preprocessor directive. It may give warning though. Preprocessor takes the most recent value before use of and put it in place of a.
Question 9
CORRECT
Output?
#include<stdio.h>
#define f(g,g2) g##g2
int main()
{
    int var12 = 100;
    printf ( "%d" , f(var,12));
    return 0;
}
100
B
Compiler Error
C
0
D
1

Discuss it


Question 9 Explanation: 
The operator ## is called “Token-Pasting” or “Merge” Operator. It merges two tokens into one token. So, after preprocessing, the main function becomes as follows, and prints 100.
int main() 
{ 
   int var12 = 100; 
   printf("%d", var12); 
   return 0; 
}
Question 10
CORRECT
Which file is generated after pre-processing of a C program?
A
.p
.i
C
.o
D
.m

Discuss it


Question 10 Explanation: 
After the pre-processing of a C program, a  .i file is generated which is passed to the compiler for compilation.
Question 11
CORRECT
What is the use of "#pragma once"?
Used in a header file to avoid its inclusion more than once.
B
Used to avoid multiple declarations of same variable.
C
Used in a c file to include a header file at least once.
D
Used to avoid assertions

Discuss it


Question 12
CORRECT
Predict the output of following program?
#include <stdio.h>
#define MAX 1000
int main()
{
    int MAX = 100;
    printf ( "%d " , MAX);
    return 0;
}
A
1000
B
100
Compiler Error
D
Garbage Value

Discuss it


Question 12 Explanation: 
After preprocessing stage of compilation, the function main() changes to following
int main()
{
   int 1000 = 100;  // COMPILER ERROR: expected unqualified-id before numeric constant
   printf("%d ", 1000);
   return 0;
}
Question 13
CORRECT
Output of following C program?
#include<stdio.h>
#define max abc
#define abc 100
 
int main()
{
     printf ( "maximum is %d" , max);
     return 0;
}
maximum is 100
B
abcimum is 100
C
100imum is 100
D
abcimum is abc

Discuss it


Question 14
CORRECT
#include <stdio.h>
#define get(s) #s
 
int main()
{
     char str[] = get(GeeksQuiz);
     printf ( "%s" , str);
     return 0;
}
A
Compiler Error
B
#GeeksQuiz
GeeksQuiz
D
GGeeksQuiz

Discuss it


Question 14 Explanation: 
The preprocessing operator '#' is used to convert a string argument into a string constant.
You have completed 14/14 questions .
Your score is 64%.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值