Computer Sciences > GATE 2026 SET-1 > C Programming
Consider the following program in C:
#include <stdio.h>
void func(int i, int j) {
if(i < j) {
int i = 0;
while (i < 10) {
j += 2;
i++;
}
}
printf("%d", i);
}
int main() {
int i = 9, j = 10;
func(i, j);
return 0;
}

The output of the program is _________. (answer in integer)
Note: Assume that the program compiles and runs successfully.

Correct : 9

The key concept here is variable shadowing and scope in C.
func is called with i=9 and j=10 as parameters. The condition i < j checks 9 < 10 which is true, so we enter the if block.
Inside the if block, int i = 0 declares a brand new local variable also named i. This inner i is completely separate from the function parameter i — it simply shadows it within the if block''s scope. Any reference to i inside the if block now refers to this inner i, not the parameter.
The while loop runs with this inner i starting from 0. Each iteration increments the inner i by 1 and adds 2 to j. After 10 iterations the inner i reaches 10 and the loop exits. j becomes 30, but j is a local copy passed by value so this doesn''t affect anything outside.
Once the if block ends, the inner i goes out of scope and is destroyed. The printf statement that follows is outside the if block, so the name i now refers back to the original function parameter i, which was never touched — it is still 9.
Output: 9 ✓

Similar Questions

Consider the following two functions void fun1(int n){ &nbsp; &nbsp; if(n == 0) return; &nbsp; &nbsp; &nbsp; printf(“%d”, n); &nbsp; &nbsp; fun2(n-2); &...
#175 MCQ
Consider the C functions foo and bar given below: int foo(int val) { &nbsp; &nbsp; int x = 0; &nbsp; &nbsp; while (val > 0) &nbsp; &nbsp; { &nbsp; &nbsp; &nbs...
#190 MCQ
What is printed by the following ANSI C program? #include&lt;stdio.h&gt; int main(int argc, char *argv[]) { &nbsp; &nbsp; int a[3][3][3] = &nbsp; &nbsp; {{...
#311 MCQ

Related Topics

variable shadowing scope C program GATE 2026 GATE CS 2026 Set-1 Q41 C variable scope shadowing output GATE inner variable shadows parameter C GATE NAT C programming output GATE 2026 local variable scope if block C GATE

Unique Visitor Count

Total Unique Visitors

Loading......