(Corrected) if3a.c

#include <stdio.h>

int main()
{
   int x;
   printf("Input x: ");
   scanf("%d", &x);
   if (x < 0)
   {
       printf("x is negative.\n");
   } else
   {
       if (x == 0)
       {
           printf("x is zero.\n");
       } else
       {
           printf("x is positive.\n");
       }
   }
   return 0;
}
  • There you go.
  • Notice the == symbol. This symbol is used in equality comparison. Whereas the single equal sign (=) is used to assign variables.
  • Remember this! This can lead to a simple but frustating mistake for beginning C/C++ programmers.
  • If you're in doubt, try to replace == with = and see what happens. The strange thing is that it compiles without complaint! Try to run it and input for a positive number. Surprise!
  • Note that some newer compiler can detect this mistake and give you warnings.

 


Where to go

Chapter 2
News Page
Lesson 1 contents
Contacting Me


Roby Joehanes © 2001