Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Ok, so I'm new to Ubuntu and Linux OS. So far I'm loving it, specially the CLI. I also like programming but I'm a self learner, pretty new in this too. I wrote a small program using VIM and compiled it using GCC. No problems there, but when I run the executable file, the output isn't what I expected the program to do, the if statement doesn't evaluate the conditions and it prints all the options as if they were all true. Here's the code and the result:

#include <stdio.h>

int main()

{
   int num1;
   int num2;

   printf( "Relations between two numbers.\n" );
   printf( "Please, enter two numbers,separated by a space: " );

   scanf( "%d%d", &num1, &num2 );

   if ( num1 == num2 ); 
      {
       printf( "%d is equal than  %d\n", num1, num2 );
      }

   if ( num1 != num2 ); 
      {
       printf( "%d is different than %d\n", num1, num2 );
      }

   if ( num1 < num2 ); 


    {
       printf( "%d is less than %d\n", num1, num2 );
      }

   if ( num1 > num2 ); 
      {
       printf( "%d is greater than %d\n", num1, num2 );
      }

   if ( num1 <= num2 ); 
      {
       printf( "%d is less or equal than %d\n", num1, num2 );
      }



if ( num1 >= num2 ); 
      {
       printf( "%d is greater or equal than %d\n", num1, num2 );
      }

return 0;
}

When I run it, I get the following:

Relations between two numbers.
Please, enter two numbers, separated by a space: 34 45
34 is equal than  45
34 is different than 45
34 is less than 45
34 is greater than 45
34 is less or equal than 45
34 is greater or equal than 45

As you can see, it printed out all the conditions, without evaluating them. When I try the same code, in a Windows OS using Dev C++ (which also uses GCC), it runs perfectly.

So, what am I doing wrong? I can write all my programs in Windows but what I really want is to become an Ubuntu user and get rid of Microsoft for good ;) Any help would be greatly appreciated, thanks in advance!!

share|improve this question

closed as off topic by Alvar, Lekensteyn, AgentCool, Mahesh, Mitch May 27 '13 at 15:55

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

6  
It's because you have stray semicolons after each if(). –  Mechanical snail May 27 '13 at 8:04
2  
If you're a self learner, you'll find more interesting questions & answers on stackoverflow.com –  Xavier T. May 27 '13 at 9:33
1  
A good IDE would catch this mistake immediately... –  BlueRaja - Danny Pflughoeft May 27 '13 at 10:10
5  
Clearly off topic here: stackoverflow.com is the platform you're looking for. –  stefan May 27 '13 at 13:15

3 Answers 3

The semi colon after the if statement ends the statement so:

if ( num1 == num2 ); 
   {
   printf( "%d is equal than  %d\n", num1, num2 );
   }

is the same as

if ( num1 == num2 ){
    ; /* Do nothing */
}
printf( "%d is equal than  %d\n", num1, num2 );

An if statement does the bit following the condition if the condition is met

This will work

if ( num1 == num2 ){
    printf( "%d is equal than  %d\n", num1, num2 ); 
}

And so will this

if ( num1 == num2 )
{
    printf( "%d is equal than  %d\n", num1, num2 ); 
}

or this

if ( num1 == num2 ) printf( "%d is equal than  %d\n", num1, num2 ); 

The brackets { } are only needed if you want more than one instruction to be executed if the condition is met.

However since C is compiled language there is no reason not to always use brackets. They can make your code easier to read.

share|improve this answer

Remove semicolons after if statements.

share|improve this answer
#include <stdio.h>

int main()

{
   int num1;
   int num2;

   printf( "Relations between two numbers.\n" );
   printf( "Please, enter two numbers,separated by a space: " );

   scanf( "%d%d", &num1, &num2 );

   if ( num1 == num2 ){
      printf( "%d is equal than  %d\n", num1, num2 );
   }

  if ( num1 != num2 ){
     printf( "%d is different than %d\n", num1, num2 );
  }

  if ( num1 < num2 ){
     printf( "%d is less than %d\n", num1, num2 );
  }

  if ( num1 > num2 ){
     printf( "%d is greater than %d\n", num1, num2 );
  }

  if ( num1 <= num2 ){
     printf( "%d is less or equal than %d\n", num1, num2 );
  }

  if ( num1 >= num2 ){
     printf( "%d is greater or equal than %d\n", num1, num2 );
  }

  return 0;
}

You were making wrong use of the semicolons.

share|improve this answer
    
Thanks all for your help, it was soooo simple that now I feel silly but hey, it's always best to ask, right? :) –  user162124 May 27 '13 at 18:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.