GateAI
Programming in C

GATE CSE 2018 · Q2

MCQ1 markEasy

Time

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:

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: A) 0, c

Programming in C
  1. The structure Ournode contains three char variables: x, y, and z, each taking 1 byte in contiguous memory.

  2. In main(), variable p is initialized as:

    • p.x = '1' (at offset 0)

    • p.y = '0' (at offset 1)

    • p.z = 'a' + 2 = 'c' (at offset 2, since 'a' + 2 is the character 'c')

  3. Pointer q points to p. Casting q to (char*) yields a pointer to the first byte of p (i.e., p.x).

  4. *((char*)q + 1) dereferences byte at offset 1, which contains '0'.

  5. *((char*)q + 2) dereferences byte at offset 2, which contains 'c'.

  6. The printf format string "%c, %c" outputs these characters separated by a comma and a space, producing 0, c.

Therefore, (A) is the correct option.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q2 of the GATE CSE 2018 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 20 of them are 1-mark MCQs like this one, and GATE CSE 2018 has 2.

How GATE tests Programming in C

More from GATE CSE 2018

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2018 · Q2 · MCQ

Consider the following C program. #include 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:

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.