ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int value = 100;
void modify(int value) {
value = 200;
}
int main(void) {
modify(value);
printf("%d", value);
}- A. 200
- B. 100
- C. Undefined behaviour: value is ambiguous between the parameter and the global
- D. Compile-time error: parameter name conflicts with global variable
Correct answer: B. 100
modify's parameter value is a completely separate local variable that merely happens to share the global's NAME; it shadows the global only within modify's own body, and since C passes arguments by value, modifying the parameter never affects the caller's actual argument or the global. main's own global value stays 100.
More C Programming questions
- 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);
- What is printed? c int i = 0; int arr[5] = {10, 20, 30, 40, 50}; while (i < 5) printf("%d ", arr[i++]); printf("%d", i);