LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 1

Output of if (x = 5) in C Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int x = 0;
    if (x = 5)
        printf("yes %d\n", x);
    else
        printf("no %d\n", x);
    return 0;
}
  • A yes 5 Correct
  • B no 0
  • C no 5
  • D yes 0
Correct Answer: Option A

x = 5 stores 5 in x and evaluates to 5, which is true. The comparison x == 5 was probably intended – compilers warn about this line.

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