ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
static int privateCounter = 0;
void bump(void) { privateCounter++; }
int getCounter(void) { return privateCounter; }
int main(void) {
bump();
bump();
printf("%d", getCounter());
}- A. 2
- B. 0
- C. 1
- D. Compile-time error: privateCounter cannot be accessed by two different functions
Correct answer: A. 2
static at file scope gives privateCounter internal linkage (confined to this translation unit), but it is still an ordinary, freely-shared global WITHIN that same file — any function in the file, including bump() and getCounter(), can read and modify it normally. Two bump() calls bring it to 2.
More C Programming questions
- What is printed? c int value = 100; void modify(int value) { value = 200; } int main(void) { modify(value); printf("%d", value); }
- What is printed? c int a = 1; int b = a++ + a++; int c = ++a + ++a; printf("UB UB");
- What is printed? c int a = 5; int b = 10; int result = a > b ? a : b > 0 ? b : 0; printf("%d", result);
- What is printed? c int x = 5; int y = (x = 10) + (x = 20); printf("%d", y);
- What is printed? c int a = 1, b = 2; int result = !a == !b; printf("%d", result);
- What is printed? c int x = 10; int y = x > 5 ? x > 8 ? 1 : 2 : 3; printf("%d", y);