Machine-translated by Google. Questions are authored in English.
Top

C Preprocessor Questions and Answers

61.

What will be the output of the following C code?

#include <stdio.h>
#define hello 
main()
{
    #ifdef hello
    #define hi 4
    #else
    #define hi 5
    #endif
    printf("%d",hi);
 
}

 

Answer: A

The code shown above illustrates #define, #ifdef, #else, #endif. Since the identifier hello is defined (condition of #if is true), another identifier names hi is defined and it’s value is 4. If hello was not defined, then the value of hi would be 5.

Please log in or register to report this question.

Answer: C

Correct answer: It causes the preprocessor to report a fatal error

#error message makes the preprocessor stop with a diagnostic. It is used to fail loudly when a configuration makes no sense - for example #if !defined(__STDC__) followed by #error This code requires a standard C compiler - rather than letting the build produce something broken.

Please log in or register to report this question.

63.

What will be the output of the following C code?

#include <stdio.h>
#define max 20
main()
{
    #ifndef max
    #define min 10
    #else
    #define min 30
    #endif
    printf("%d",min);
}

 

Answer: C

Correct answer: 30

max is defined, so #ifndef max is false and the #else branch is taken, defining min as 30. The program prints 30.

Please log in or register to report this question.

64.

What will be the output of the following C code?

#include <stdio.h>
#define hello 10
main()
{
    #ifdef hello
    #undef hello
    #define hello 100
    #else
    #define hello 200
    #endif
    printf("%d",hello);
}

 

Answer: C

Correct answer: 100

hello is defined, so the #ifdef branch runs: the old definition is removed with #undef and a new one, 100, put in its place. Using #undef before redefining is exactly the right way to do this, since it avoids the redefinition warning. The output is 100.

Please log in or register to report this question.

65.

What will be the output of the following C code?

#include <stdio.h>
#define sf 10
main()
{
    if(sf==100)
        printf("good");
    else
    {
        printf("bad");
        sf=100;
    }
    printf("%d",sf);
}

 

Answer: D

The above code will result in an error because a macro cannot be defined using a simple assignment operator. In the code shown above, the value of sf is changed to 100 using the assignment operator. This causes an error.

Please log in or register to report this question.

66.

What will be the output of the following C code?

#include <stdio.h>
void f()
{
    #define sf 100
    printf("%d",sf);
}
int main()
{
    #define sf 99;
    f();
    printf("%d",sf);
}

 

Answer: A

Correct answer: error

The macro body is 99; - the semicolon is part of the text. Substituting it gives printf("%d",99;);, a syntax error. (The redefinition of sf without an intervening #undef is a second fault.) A #define for a constant should never end in a semicolon.

Please log in or register to report this question.

67.

What will be the output of the following C code?

#include <stdio.h>
#define INDIA 1
#define US 2
#define CC US
main()
{
    #if CC==INDIA
    printf("Rupee");
    #elif CC==US
    printf("Dollar");
    #else
    printf("Euro");
    #endif 
}

 

Answer: C

Correct answer: Dollar

CC expands to US, which expands to 2 - expansions are rescanned. The first test 2 == 1 fails, the #elif test 2 == 2 succeeds, so printf("Dollar") is the only line that reaches the compiler.

Please log in or register to report this question.

68.

What will be the output of the following C code?

#define sqr(x) x*x
main()
{
    int a1;
    a1=25/sqr(5);
    printf("%d",a1);
}

 

Answer: A

The output of the code shown above is 25. This is because the arithmetic statement takes the form of: 25/5*5 (which is equal to 1). If the macro had been defined as #define sqr(x) (x*x), the output would have been 1.

Please log in or register to report this question.

69.

Which of the following is not a preprocessor directive?

Answer: D

Correct answer: #ifelse

There is no #ifelse directive. The conditional set is #if, #ifdef, #ifndef, #elif, #else and #endif - the “else if” form is spelt #elif. #error and #pragma are both real directives.

Please log in or register to report this question.

70.

Which of the following are C preprocessors?

Answer: D

Correct answer: all of the mentioned

#ifdef, #define and #endif are all preprocessor directives - the first and third belonging to conditional compilation and the second to macro definition. Every directive begins with # and is processed before the compiler proper sees the file.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: C Preprocessor Questions and Answers || C Preprocessor MCQ Questions and Answers || C Preprocessor GK Questions and Answers || C Preprocessor GK MCQ Questions || C Preprocessor Multiple Choice Questions and Answers || C Preprocessor GK || GK on C Preprocessor || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming