LearnFrenzy
Sign in Take a free test

Control Instructions · Control Instructions MCQ · Question 2

if (x = 5) – what happens in C? Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What does if (x = 5) { … } do?

  • A Compares x with 5
  • B Assigns 5 to x, and the body always runs because 5 is non-zero Correct
  • C It does not compile
  • D Assigns 5 only if x was 0
Correct Answer: Option B

= is assignment; its value is the value assigned (5), which is true. Compilers warn ("using the result of an assignment as a condition"). Use == to compare.

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