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

Pointers Questions and Answers

91.

What is the output of this C code?

int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}

 

Answer: D

Correct answer: 0.000000

The cast changes how the bytes are interpreted, not what they contain. The four bytes hold the integer 10, and read as an IEEE single-precision float that bit pattern is a denormal of about \( 1.4 \times 10^{-44} \) - which %f prints, to six places, as 0.000000. The program compiles because the cast is explicit; it is the meaning rather than the syntax that is wrong.

Please log in or register to report this question.

92.

What will be the output of the following C code?

#include <stdio.h>
    void foo(float *);
    int main()
    {
        int i = 10, *p = &i;
        foo(&i);
    }
    void foo(float *p)
    {
        printf("%f\n", *p);
    }

 

Answer: B

Correct answer: 0.000000

foo expects a float * but is given an int *. The pointer types are incompatible, so a conforming compiler must diagnose this - but the code compiles with a warning on most implementations, and the resulting behaviour is what the question asks about.

Inside foo, the four bytes holding the integer 10 are reinterpreted as a float. The bit pattern 0x0000000A read as an IEEE single is a denormal of the order of \( 10^{-44} \), which prints as 0.000000 at the default precision. The value is an accident of representation, not a conversion.

Please log in or register to report this question.

93.

Will the program compile?

#include <stdio.h>
int main()
{
    char str[5] = "IndiaBIX";
    return 0;
}

 

Answer: A

C doesn't do array bounds checking at compile time, hence this compiles.

But, the modern compilers like Turbo C++ detects this as 'Error: Too many initializers'.

GCC would give you a warning.

Please log in or register to report this question.

94.

What is the output of this C code?

int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int j = 10;
return &j;
}

 

Answer: A

Correct answer: Undefined behaviour

j is an automatic variable whose lifetime ends when f returns, so the address returned is dangling and dereferencing it is undefined. It very often prints 10, because the stack frame has not yet been overwritten, but that is an accident rather than a guarantee - another call, a different optimisation level or a different compiler can produce anything at all. The preceding question shows the correct pattern, using malloc.

Please log in or register to report this question.

Loading…
Tags: Pointers Questions and Answers || Pointers MCQ Questions and Answers || Pointers GK Questions and Answers || Pointers GK MCQ Questions || Pointers Multiple Choice Questions and Answers || Pointers GK || GK on Pointers || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming