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

Discussion

Consider the following C program.

#include\(\lt\)stdio.h\(\gt\)
struct Ournode{
    char x,y,z;
};

int main(){
    struct Ournode p = {'1', '0', 'a'+2};
    struct Ournode *q = &p;
    printf ("%c, %c", *((char*)q+1), *((char*)q+2));
    return 0;
}

The output of this program is:

  • A.0, c
  • B.0, a+2
  • C.'0', 'a+2'
  • D.'0', 'c'

Answer: A

The program prints 0, c.

The structure holds three chars in order, x = '1', y = '0' and z = 'a'+2. Since 'a'+2 is evaluated as a character code, z holds 'c'. Casting q to char* and adding 1 and 2 points at y and z, so the two values printed are '0' and 'c'.

The format %c prints just the character, without quotes, so the options with quotes are wrong. 'a+2' would need the expression to be stored as text, but it is computed when the structure is initialised.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.