ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int f(void) { printf("f "); return 1; }
int g(void) { printf("g "); return 1; }
int main(void) {
if (f() || g())
printf("yes ");
}- A. f yes
- B. g yes
- C. f g yes
- D. g f yes
Correct answer: A. f yes
|| guarantees left-to-right evaluation with short-circuiting: f() is called first, printing "f ", and returns 1 (true), so g() is never evaluated at all (the right side of || is skipped once the left side is truthy); the condition is true, so "yes " prints.
More C Programming questions
- What is printed? c int globalVar = 100; void modify(void) { int globalVar = 200; globalVar += 5; printf("local:%d ", globalVar); } int…
- What is printed? c int a = 3; int result = a++ 2 + --a; printf("%d %d", a, result);
- What is printed? c // header.h (included by both files) extern int sharedValue; c // a.c #include "header.h" int sharedValue = 42; c //…
- What is printed? c int a = 5, b = 2, c; c = a > b ? a++ : --b; printf("%d %d %d", a, b, c);
- What is printed? c int a = 2; int b = (a++, a + 3, a 2); printf("%d %d", a, b);
- What is printed? c int a = 10; int b = a++ + a++; printf("%d", a);