C program - trouble with IF statement

1. Jul 2, 2004

Math Is Hard

Staff Emeritus
I feel silly asking this but I can't get my "If" statement to work correctly. This should print "a is NOT equal to b" but it prints "a is equal to b".
Any help? Thanks!!!

#include <stdio.h>
int main(void)
{
int a = 10;
int b = 15;

if(a == b)
printf("a is equal to b \n");
else
printf("a is NOT equal to b \n");

return 0;
}

2. Jul 2, 2004

AKG

Try using more brace brackets

#include <stdio.h>
int main(void)
{
int a = 10;
int b = 15;

if(a == b)
{printf("a is equal to b \n");}
else
{printf("a is NOT equal to b \n");}

return 0;
}

3. Jul 2, 2004

robphy

works for me:

Code (Text):

#include <stdio.h>

int main(void)
{
int a = 10;
int b = 15;

if(a == b)
printf("a is equal to b \n");
else
printf("a is NOT equal to b \n");

return 0;
}

$gcc _test.c$ ./a.exe
a is NOT equal to b

4. Jul 2, 2004

e(ho0n3

Another tip: Make sure you are have a == b and not a = b in the code.

5. Jul 2, 2004

e(ho0n3

One more thing: Get a debugger. You can see what is happening when you step through the code using a debugger than using any other method.

6. Jul 2, 2004

Math Is Hard

Staff Emeritus
I do have double equals. I cut and pasted exactly what I am trying to run.

Thanks.

7. Jul 2, 2004

Math Is Hard

Staff Emeritus
bizarre! I create a new.c file and pasted the code back in and now the logic works fine!
I guess it will remain a mystery!

thanks for running that on your end, robphy.

8. Jul 2, 2004

Math Is Hard

Staff Emeritus
I'll keep that in mind - thanks!