GATE CS 2025 Set 2 — Question 62
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.NAT+2 / -0MediumPointers & Pointer ArithmeticC ProgrammingProgramming & Data StructuresArrays & Strings in C
Programming & Data Structures → C Programming → Arrays & Strings in C
Last updated
Question
Consider the following C program:The output of the above program is ___________.
#include <stdio.h>
int main() {
int a;
int arr[5] = {30, 50, 10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}
Correct answer
111 to 111
Solution
Let's trace the execution of the C program step by step:
The array
So,
Dereferencing
So,
This increments the value at the memory location
Current state of array:
The pointer
Let's evaluate the expression
The output is 111.
1.Initialization:
int arr[5] = {30, 50, 10};
The array
arr is initialized. The first three elements are specified, and the remaining are zero-initialized (standard C behavior).arr[0] = 30arr[1] = 50arr[2] = 10arr[3] = 0arr[4] = 02.Pointer Assignment:
ptr = &arr[0] + 1;
&arr[0] is the address of the first element. Adding 1 moves the pointer to the next integer in the array.So,
ptr now points to arr[1].3.Value Assignment to
a: a = *ptr;
Dereferencing
ptr gives the value of arr[1], which is 50.So,
a = 50.4.Incrementing the Value Pointed to by
ptr: (*ptr)++;
This increments the value at the memory location
ptr is pointing to (arr[1]).arr[1] changes from 50 to 51.Current state of array:
{30, 51, 10, 0, 0}.5.Incrementing the Pointer:
ptr++;
The pointer
ptr is incremented to point to the next element.ptr now points to arr[2].6.Print Statement:
printf("%d", a + (*ptr) + arr[1]);
Let's evaluate the expression
a + (*ptr) + arr[1]:-
a: The value stored inais 50 (from step 3). -
*ptr:ptrpoints toarr[2], so*ptris 10. -
arr[1]: The value ofarr[1]is currently 51 (modified in step 4).
The output is 111.
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]