LearnFrenzy
Sign in Take a free test

Operators and Expressions · Find the Error · Question 2

Undefined behaviour: printf("%d %d", i++, i++) Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    int i = 1;
    printf("%d %d\n", i++, i++);
    return 0;
}
  • A printf cannot take expressions
  • B Both arguments modify i, and argument evaluation is unsequenced – undefined behaviour Correct
  • C i must be initialised to 0
  • D There is no bug; it always prints 1 2
Correct Answer: Option B

The order in which arguments are evaluated is unspecified, and two unsequenced modifications of the same object are undefined. Compute the values in separate statements first.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links