LearnFrenzy
Sign in Take a free test

Operators and Expressions · Find the Error · Question 5

Error: applying ++ to an expression that is not a variable Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

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

main.c
#include <stdio.h>

int main(void) {
    int a = 2, b = 3;
    int c = (a + b)++;
    printf("%d\n", c);
    return 0;
}
  • A c must be declared first
  • B ++ needs a modifiable lvalue (a variable); a + b is just a value Correct
  • C Brackets cannot be used with ++
  • D There is no bug; it prints 5
Correct Answer: Option B

Increment and assignment change an object, so their operand must designate one. a + b is a temporary value. Write int c = a + b + 1;.

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