C program - trouble with IF statement

Click For Summary

Homework Help Overview

The discussion revolves around a C programming issue related to the functionality of an "if" statement. The original poster is experiencing unexpected output when comparing two integer variables.

Discussion Character

  • Exploratory, Assumption checking, Problem interpretation

Approaches and Questions Raised

  • Participants discuss the use of brace brackets in the "if" statement, question the equality operator versus the assignment operator, and suggest using a debugger to investigate the issue further.

Discussion Status

Some participants have provided suggestions regarding coding practices, while others have confirmed that the code works correctly in their own environments. The original poster expresses confusion despite following the advice given.

Contextual Notes

The original poster mentions that they have double equals in their code, indicating a potential misunderstanding or oversight in their setup. There is also a mention of the code working correctly when pasted into a new file, suggesting possible environmental issues.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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;
}
 
Physics news on Phys.org
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;
}
 
works for me:

Code:
#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
 
Another tip: Make sure you are have a == b and not a = b in the code.
 
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.
 
e(ho0n3 said:
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.
 
robphy said:
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.
 
AKG said:
Try using more brace brackets

I'll keep that in mind - thanks!
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K