PDA

View Full Version : C program - trouble with IF statement


Math Is Hard
Jul2-04, 03:41 PM
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;
}

AKG
Jul2-04, 03:53 PM
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;
}

robphy
Jul2-04, 03:56 PM
works for me:



#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

e(ho0n3
Jul2-04, 03:57 PM
Another tip: Make sure you are have a == b and not a = b in the code.

e(ho0n3
Jul2-04, 03:59 PM
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.

Math Is Hard
Jul2-04, 05:03 PM
Another tip: Make sure you are have a == b and not a = b in the code.

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

Thanks.

Math Is Hard
Jul2-04, 05:09 PM
works for me:



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.

Math Is Hard
Jul2-04, 05:11 PM
Try using more brace brackets



I'll keep that in mind - thanks!