GateAI
Programming in C

GATE CSE 2024 Set 2 · Q36

MCQ2 marksMedium

Time

What is the output of the following C program?#include <stdio.h> int main() { double a[2]={20.0, 25.0}, *p, *q; p = a; q = p + 1; printf("%d,%d", (int)(q - p), (int)(*q - *p)); return 0; }

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) 1,5

Programming in C

Let us trace the given C program step-by-step:

  1. Array and Pointer Initialization:

    • a is an array of two double elements: a[0] = 20.0 and a[1] = 25.0.

    • p and q are pointers of type double*.

    • p = a; makes pointer p point to the first element a[0] (address &a[0]).

    • q = p + 1; performs pointer arithmetic, making pointer q point to the next double element a[1] (address &a[1]).

  2. Evaluating (int)(q - p):

    • In C, pointer subtraction (q - p) between two pointers of the same type yields the number of elements of that base type separating the two pointers, not the byte distance.

    • Since q points to a[1] and p points to a[0], the pointer difference is 1.

    • Casting to int gives 1.

  3. **Evaluating (int)(*q - *p):**

    • Dereferencing *q gives 25.0 and *p gives 20.0.

    • The arithmetic difference is 25.0−20.0=5.025.0 - 20.0 = 5.0.

    • Casting to int gives 5.

  4. Output:

    • The printf format string is "%d,%d", which prints 1,5.

Hence, the correct option is (B).

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 Q36 of the GATE CSE 2024 Set 2 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. 19 of them are 2-mark MCQs like this one, and GATE CSE 2024 Set 2 has 4.

How GATE tests Programming in C

More from GATE CSE 2024 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2024 Set 2 · Q36 · MCQ

What is the output of the following C program? #include int main() { double a[2]={20.0, 25.0}, *p, *q; p = a; q = p + 1; printf("%d,%d", (int)(q - p), (int)(*q - *p)); return 0; }

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.