ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int f(void) { printf("f "); return 1; }
int g(void) { printf("g "); return 0; }
int main(void) {
    if (g() && f())
        printf("both ");
    else
        printf("short ");
}
  1. A. f g short
  2. B. g f both
  3. C. g short
  4. D. g f short

Correct answer: C. g short

&& short-circuits: g() is called first (printing "g ") and returns 0 (false), so f() is never called at all (short-circuit evaluation is a GUARANTEED sequence point/ordering, unlike the earlier unsequenced examples); the if-condition is false, so "short " prints.

More C Programming questions

Exams that ask this topic