GATE CSE 2019 Q63: Output of a C pointer arithmetic loop over an array
- Programming and Data Structures
- 2 marks
- NAT
- No negative marking
Syllabus topic: Programming in C
Question
Consider the following C program:
#include <stdio.h>
int main()
{
int a[] = {2, 4, 6, 8, 10};
int i, sum = 0, *b = a + 4;
for (i = 0; i < 5; i++)
sum = sum + (*b - i) - *(b - i);
printf ("%d\n", sum);
return 0;
}The output of the above C program is ________.
Try it: numerical answer: type the value
Numerical answer. No negative marking.
Show answer and explanation
Answer: 10 (official GATE 2019 final answer key)
Concept used: subtracts from the value points to, while reads the element positions before ; the brackets decide whether arithmetic is on the value or on the pointer.
First step: Note b points to a[4] = 10, so each term is ; evaluate it for to and add.
Full step-by-step solution
Loading…
Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.
More questions on Programming in C
Every PYQ on this topic →Share this question
Source: official GATE 2019 CS question paper and final answer key, organised by IIT Madras. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15