The GATE Grind

GATE 2018 CS – Question 12

Programming and Data Structures · Programming in C · 1 mark · Multiple choice

Consider the following C program.

#include<stdio.h>
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:

  1. 0, c
  2. 0, a+2
  3. '0', 'a+2'
  4. '0', 'c'

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) 0, c

Explanation

The three chars are stored consecutively: `x='1'`, `y='0'`, `z='c'`. `(char*)q+1` is `y` and `(char*)q+2` is `z`, so it prints `0, c` (printf with `%c` does not print quotes).