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

C Preprocessor Questions and Answers

101.

Identify the macro(s) defined in stdarg.h.

Answer: D

Correct answer: all the above

<stdarg.h> defines all three: va_start to begin walking the variable argument list, va_arg to fetch the next argument (given its type), and va_end to finish. They are what a function such as printf uses to reach arguments the compiler cannot type-check.

Please log in or register to report this question.

102.

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.

103.

Undefined function calls in a C program are detected

Answer: C

Correct answer: by the linker

The compiler is satisfied by a declaration: it only needs to know the function’s signature to generate a call. Whether a definition actually exists is settled when the object files and libraries are combined, so a missing function surfaces as a linker error - the familiar “undefined reference”.

Please log in or register to report this question.

104.

There exists a way to prevent the same file from getting #included twice in the same program.

Answer: A

Correct answer: True

That is what an include guard does. The header wraps its contents in #ifndef HDR_H / #define HDR_H / #endif, so on any later inclusion the macro is already defined and the body is skipped. #pragma once achieves the same thing in one line, though it is a compiler extension rather than standard C.

Please log in or register to report this question.

105.

Macros have a local scope.

Answer: B

Correct answer: False

A macro has no scope in the C sense at all. Its definition runs from the #define to the end of the file, or to a matching #undef - braces and functions mean nothing to the preprocessor, which works on text. A #define written inside a function is still in force after that function ends.

Please log in or register to report this question.

106.

What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?

#include <stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
    int x=10, y=20;
    SWAP(x, y, int);
    printf("%d %d\n", x, y);
    return 0;
}

 

Answer: C

Correct answer: Not compile

The macro expands to

(int t; t=x, x=y, y=t);

which puts a declaration inside a parenthesised expression - something C does not allow. The trick of passing a type as a macro argument works only if the expansion places the declaration where a declaration may appear, typically inside a do { … } while(0) block.

Please log in or register to report this question.

107.

#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?

Answer: A

Correct answer: Geeks

The macro expands to X == 0, and inside a #if the undefined identifier X becomes 0. The condition is therefore 0 == 0, which is true, so printf("Geeks") is the line that survives preprocessing.

Please log in or register to report this question.

109.

Preprocessor directive #undef can be used only on a macro that has been #define earlier

Answer: A

Correct answer: True

#undef does one thing: it removes an existing macro definition. Applied to a name that was never defined it has nothing to remove and is simply ignored, so the directive is meaningful only for a macro defined earlier. Its usual purpose is to redefine a macro safely - #undef then #define - or to stop a macro shadowing a real function name.

Please log in or register to report this question.

110.

Point out the error in the program

#include <stdio.h>
#define SI(p, n, r) float si; si=p*n*r/100;
int main()
{
    float p=2500, r=3.5;
    int n=3;
    SI(p, n, r);
    SI(1500, 2, 2.5);
    return 0;
}

 

Answer: C

Correct answer: Error: Multiple declaration of si

The macro body declares si, so using it twice in the same block expands to two float si; declarations of the same name in the same scope - which the compiler rejects. This is the standard argument for wrapping a multi-statement macro in do { … } while(0), which gives each expansion its own block and its own scope.

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