Learn to Code
John F. Dumas
contact me | resume | how it works | example programs | testimonials | main page

Help Example: Two

one | two | three | four | five

► QUESTION: I am trying to write a simple C program that asks the user to enter an integer. If the number entered by the user is 7, the program is supposed to respond by saying "Excellent!", if the number is not a seven, the number should say "Too Bad!".

The problem is that the program says: "Excellent!" no matter what number I enter. Can you help me figure out what is happening? Here is my code:

   #include <stdio.h>

   int main()
      int number;

      printf("Please enter an integer: ");

      scanf("%d", &number);

      if(number = 7)

         printf("Too Bad!\n");


► ANSWER: The problem here is a common one for newer programmers to encounter. In 'C' there is a distinction between:

   a) Assignment

       int x = 42;

   b) Equality

      if(x == 42)

In your program, you are intending to test to see if 'number' is seven, but you are actually assigning 7 to 'number' and then testing to see if 'number' is true or false (non-zero or zero). The sequence goes like this:

      if(number = 7)

         1) First, we assign 7 to 'number' so the
            'if' statement is equivalent to:


         2) Now, since 7 is non-zero, it's treated as 'true'
            which is why we always see:


Fixing this problem ie easy enough, just change:

      if(number = 7)


      if(number == 7)

But in situations like this, it's helpful to "fix the bug twice". What I mean is to not only correct the problem but to also address the circumstances that led to the problem. Because this assignment/equality issue is so common, compilers tend to be able to spot it if they are told to do so. If we were to compile the program above with full warnings enabled, what would we see? Here's the output from two different compilers with warnings enabled:

   Visual C (/W4)

      seven.c(11) : warning C4706: assignment within conditional expression

   GCC (-Wall)

      seven.c:11: warning: suggest parentheses around assignment used
                           as truth value

As you can see, the compiler is more than willing to help you avoid these types of problems but it cannot force you to compile with warnings enabled - that's a habit you'll need to develop yourself.

One final note, this issue is common enough that some programmers decided to avoid it entirely by convention. Consider the following four 'if' statements:

   if(x = 5)  { ... }
   if(x == 5) { ... }  
   if(5 = x)  { ... }
   if(5 == x) { ... }

The first one is incorrect but does not result in a compiler error but notice the third 'if' statement:

   if(5 = x)  { ... }

Because '5' is not a variable, the compiler will error out when it runs into this line (a good thing!). So while these lines both compile (and the first one is wrong) ...

   if(x = 5)  { ... }
   if(x == 5) { ... }  

By using "Yoda Conditionals" (where the variable is on the right, not the left of the comparison) we made the first of these:

   if(5 = x)  { ... }
   if(5 == x) { ... }

produce a compile error rather than just quietly compiling into code with unintended consequences. By no means am I mandating you use this type of conditional (I don't tend to) but figured it was worth pointing out that there is a way to avoid this entire class of problems by using a particular coding convention.

Reference Files

© John F. Dumas | johnfdumas@gmail.com | main page | top of page