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

C Preprocessor Questions and Answers

51.

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.

52.

Consider the following statements

#define hypotenuse(a, b) sqrt (a*a+b*b) 

The macro call hypotenuse(a+2,b+3);

Answer: D

Correct answer: Find the square root of 3*a + 4*b + 5

The macro body has no parentheses round its parameters, so the arguments are pasted in as raw text:

sqrt(a+2*a+2 + b+3*b+3)

Multiplication binds tighter than addition, so this is \(\sqrt{a + 2a + 2 + b + 3b + 3} = \sqrt{3a + 4b + 5}\) - nothing like a hypotenuse. Writing #define hypotenuse(a, b) sqrt((a)*(a) + (b)*(b)) is what makes the macro behave as intended.

Please log in or register to report this question.

53.

What will be the output of the program?

#include <stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
    char *str1="India";
    char *str2="BIX";
    JOIN(str1, str2);
    return 0;
}

 

Answer: B

Correct answer: str1=India str2=BIX

Each parameter is used twice: once with #, which stringizes the argument’s text, and once plainly, which yields the pointer. So #s1 becomes "str1" while s1 is the variable holding "India". The output is str1=India str2=BIX - the idiom used by debugging macros that print both a variable’s name and its value.

Please log in or register to report this question.

54.

What is the output of this program?

#include <stdio.h>
#define i 5
int main()
{
  #define i 10
  printf("%d",i);
  return 0;
}

 

Answer: B

Correct answer: 10

The second #define appears before the printf in the file, and the preprocessor works purely on text order - the braces of main mean nothing to it, so the redefinition is in force by the time the printf line is expanded. It prints 10, with a warning about the redefinition.

Please log in or register to report this question.

55.

_______________ is the preprocessor directive which is used to end the scope of #ifdef.

Answer: C

The #ifdef preprocessor directive is used to check if a particular identifier is currently defined or not. If the particular identifier is defined, the statements following this preprocessor directive are executed till another preprocessor directive #endif is encountered. #endif is used to end the scope of #ifdef.

Please log in or register to report this question.

56.

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.

Answer: D

Correct answer: All of the mentioned

The preprocessor does all three: it handles file inclusion (#include), macro expansion (#define), and conditional compilation and line control (#if, #ifdef, #line). All of it happens on the text, before the compiler parses anything.

Please log in or register to report this question.

58.

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");
    return 0;
}

 

Answer: B

Correct answer: GeeksQuiz

## is the token-pasting operator. macro(n, a, i, m) substitutes the arguments and joins them in the order m a i n, producing the single token main. So int MAIN() becomes int main(), the program is well formed, and it prints GeeksQuiz.

Please log in or register to report this question.

59.

Will the program compile successfully?

#include <stdio.h>

int main()
{
    #ifdef NOTE
        int a;
        a=10;
    #else
        int a;
        a=20;
    #endif
    printf("%d\n", a);
    return 0;
}

 

Answer: A

Correct answer: Yes

NOTE is not defined, so the #ifdef branch is deleted and only the #else branch reaches the compiler. Exactly one declaration of a therefore survives, and the program compiles and prints 20. Had both branches been kept, the duplicate declaration would have been an error - which is the point of the example.

Please log in or register to report this question.

60.

Point out the error in the program

#include <stdio.h>

int main()
{
    int i;
    #if A
        printf("Enter any number:");
        scanf("%d", &i);
    #elif B
        printf("The number is odd");
    return 0;
}

 

Answer: A

Correct answer: Error: unexpected end of file because there is no matching #endif

Every #if, #ifdef or #ifndef must be closed by an #endif. Here the conditional started by #if A and continued by #elif B is never closed, so the preprocessor runs off the end of the file and reports it. Note that this is a preprocessing error, so it is reported before any of the C is looked at.

Please log in or register to report this question.

Loading…
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