ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int global = 1;
void modify(void) {
    int global = 2;
    global++;
}
int main(void) {
    modify();
    printf("%d", global);
}
  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 0

Correct answer: B. 1

modify declares its own local global that shadows the file-scope one entirely within its body; incrementing it only affects the local copy, which disappears when modify returns. The outer global remains 1.

More C Programming questions

Exams that ask this topic