Skip to content
Official PYQ · 2017

GATE CS 2017 Set 2 Q54 — Programming in C

Explain with AI

Consider the following C program.

#include<stdio.h>
int main () {
    int m=10;
    int n, n1;
    n=++m;
    n1=m++;
    n--;
    --n1;
    n-=n1;
    printf(“%d”, n);
    return 0;
}

The output of the program is ______

View worked solution

Answer: 0

Let us trace the execution of the program step-by-step:

  1. Initialization:
    m = 10
  2. Statement: n = ++m;
    This is a pre-increment operation.
    • m is incremented first: m = 11
    • Then, the incremented value is assigned to n: n = 11
  3. Statement: n1 = m++;
    This is a post-increment operation.
    • The current value of m (which is 11) is assigned to n1: n1 = 11
    • Then, m is incremented: m = 12
  4. Statement: n--;
    Decrement n by 1:
    n = 11 - 1 = 10
  5. Statement: --n1;
    Decrement n1 by 1:
    n1 = 11 - 1 = 10
  6. Statement: n -= n1;
    This is equivalent to n = n - n1:
    n = 10 - 10 = 0

The printf statement prints the final value of n, which is 0.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions