GATE CS 2024 Set 2 — Question 36
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+2 / -0.67MediumPointers & Pointer ArithmeticC ProgrammingProgramming & Data StructuresArrays & Strings in C
Programming & Data Structures → C Programming → Arrays & Strings in C
Last updated
Question
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;}
Correct answer
(B) 1,5
Solution
1.Pointer Arithmetic: The array
a is initialized with two double values: a[0] = 20.0 and a[1] = 25.0. The pointer p is assigned the base address of the array (p = a), so it points to a[0]. The pointer q is assigned p + 1, so it points to the next element, a[1].2.Pointer Subtraction: In C, when you subtract two pointers of the same type (
q - p), the result is the number of elements of that type between the two addresses. Since q points to the element immediately following p, q - p is . Casting this to (int) gives .3.Value Subtraction:
*q dereferences to the value , and *p dereferences to . The expression *q - *p calculates the difference between these values: . Casting this result to (int) gives .4.Final Output: The
printf function uses the format string "%d,%d", resulting in the output 1,5.Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]