Skip to content
GATEiit

GATE CSE 2019 Q63: Output of a C pointer arithmetic loop over an array

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: ∗b−i subtracts i from the value b points to, while ∗(b−i) reads the element i positions before b; the brackets decide whether arithmetic is on the value or on the pointer.

Concept notes

Loading notes…

First step: Note b points to a[4] = 10, so each term is (10−i)−a[4−i]; evaluate it for i=0 to 4 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

WhatsAppDownload image

Source: official GATE 2019 CS question paper and final answer key, organised by IIT Madras. Question paper · Answer key