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

C Preprocessor Questions and Answers

81.

What will be the output of the program?

#include <stdio.h>
#define SQUARE(x) x*x

int main()
{
    float s=10, u=30, t=2, a;
    a = 2*(s-u*t)/SQUARE(t);
    printf("Result = %f", a);
    return 0;
}

 

Answer: A

Correct answer: Result = -100.000000

The unparenthesised macro turns the expression into

a = 2*(s-u*t)/t*t;

and with / and * at equal precedence, left to right:

\[ 2 \times (10 - 60) = -100, \quad -100 / 2 = -50, \quad -50 \times 2 = -100 \]

So -100.000000, not the intended \(-100/4 = -25\).

Please log in or register to report this question.

82.

What will be the output of the program?

#include <stdio.h>
#define SQR(x)(x*x)

int main()
{
    int a, b=3;
    a = SQR(b+2);
    printf("%d\n", a);
    return 0;
}

 

Answer: B

Correct answer: 11

The macro body parenthesises the whole expression but not the parameter, so SQR(b+2) becomes (b+2*b+2):

\[ 3 + 6 + 2 = 11 \]

Only ((x)*(x)) would give the intended \(5^2 = 25\).

Please log in or register to report this question.

83.

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.

84.

What will be the output of the program?

#include <stdio.h>
#define CUBE(x) (x*x*x)

int main()
{
    int a, b=3;
    a = CUBE(b++);
    printf("%d, %d\n", a, b);
    return 0;
}

 

Answer: C

Correct answer: 27, 6

CUBE(b++) expands to (b++*b++*b++), which modifies b three times in one expression with no sequence point between - formally undefined behaviour. The listed answer, 27 and 6, is what the common compilers give: the three operands all read 3 before any increment takes effect, and b ends three higher. Nothing should rely on it; the real lesson is never to pass an argument with a side effect to a macro that uses it more than once.

Please log in or register to report this question.

85.

What will be the output of the program?

#include <stdio.h>
#define PRINT(int) printf("int=%d, ", int);

int main()
{
    int x=2, y=3, z=4;   
    PRINT(x);
    PRINT(y);
    PRINT(z);
    return 0;
}

 

Answer: A

Correct answer: int=2, int=3, int=4

A macro parameter may be named int - the preprocessor has no reserved words, and within the macro body the name simply stands for whatever was passed. Each call substitutes its own argument, so the three calls print 2, 3 and 4. The int= inside the string literal is untouched, since macro replacement never reaches inside a string.

Please log in or register to report this question.

86.

What will be the output of the program?

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

 

Answer: B

Correct answer: a = 12, b = 10

The macro expands to int t; t=a, a=b, b=t; inside main, where a declaration followed by an expression statement is perfectly legal. The comma operator sequences the three assignments left to right, so the values are exchanged and the output is a = 12, b = 10. The macro would break if used twice in the same block, since t would be declared twice.

Please log in or register to report this question.

87.

What will be the output of the program?

#include <stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%d\n", FUN(va1, 2));
    return 0;
}

 

Answer: B

Correct answer: 20

## pastes its operands into a single token, so FUN(va1, 2) becomes va12 - the name of the second variable. The printf therefore prints 20.

Please log in or register to report this question.

88.

What will be the output of the program?

#include <stdio.h>
#define MAX(a, b) (a > b ? a : b)

int main()
{
    int x;
    x = MAX(3+2, 2+7);
    printf("%d\n", x);
    return 0;
}

 

Answer: B

Correct answer: 9

The macro body parenthesises nothing, so the expansion is (3+2 > 2+7 ? 3+2 : 2+7). Addition binds tighter than >, so the comparison is \(5 > 9\), which is false, and the result is \(2+7 = 9\). Here the missing parentheses happen not to matter, but with an argument such as 1&2 they would.

Please log in or register to report this question.

89.

What will be the output of the program?

#include <stdio.h>
#define MIN(x, y) (x<y) ? x : y

int main()
{
    int x=3, y=4, z;
    z = MIN(x, y);
    if(z > 0)
        printf("%d\n", z);
    return 0;
}

 

Answer: A

Correct answer: 3

MIN(x, y) expands to (x<y) ? x : y, so with \(x = 3\) and \(y = 4\) the condition holds and z gets 3. Since 3 is greater than 0, the if succeeds and the value is printed. (The macro has no outer parentheses, so it would misbehave inside a larger expression - but the assignment here is safe.)

Please log in or register to report this question.

90.

What will be the output of the program?

#include <stdio.h>
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply

int main()
{
    char *opername = Xstr(oper);
    printf("%s\n", opername);
    return 0;
}

 

Answer: C

Correct answer: print 'multiply'

This is the standard two-level stringizing idiom. A macro argument is not expanded before # is applied, so str(oper) would give "oper". Going through Xstr first forces the argument to be expanded - oper becomes multiply - and only then is it stringized, giving "multiply".

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