If statement in C isn't evaluating the conditions
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!!
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!!
No comments:
Post a Comment