When we were doing some programming today, i wrote a good length section of code, with a few if statements thrown in there, in some of the if statements i had if(blah = blah), well i spent a good period of time trying to figure out why the code wasn’t working right, and it turns out it should have been if(blah == blah), im used to java and doing it that way, but when i compiled the first time in c with if(blah = blah) it worked fine, so i thought it was fine. there is no real problem here, i just thought to let some other people know who might be running into problems and don’t know why, to check that they have ==, not =, also what does = mean in an if statement?
Hope this helps anyone that needed it, and thanks for answering what does = mean if it isn’t the same as ==?
“=” is an assignment. It also has a value, the value assigned.
So “if (a = b)…” means, Assign the value of b to a, then if that value is true (non zero) do something.
Don’t feel bad. This is a common error even for experienced programers. The compilers don’t complain because it is legal and sometimes done on purpose.
To add on to what Don said, if you want to get the compiler to complain, you can try to get in the habit of doing the following.
if(1 == x)
{
}
instead of
if(x == 1)
{
}
With the correct comparison syntax (as written), you should see no difference when you compile, but if you were to mistakenly put a single ‘=’ instead of a double, you would see the a syntax error. This is because it tries to assign the value of x to 1. Since you don’t have permission to write to 1, you get the error.
Note that if you were to be comparing two variables, instead of a variable to a constant number, you wouldn’t see the syntax error because you would be allowed to write one variable into another (in most cases).
There are some people that use this as their coding standard. I personally don’t use it, but it may be something that helps you get in the habit of remembering the == when comparing.
In C/C++, it is common for mutators to have return values, so that you can cascade (is that the right word?) operations. For example,
a = b = c
works because the assignment operator is right-associative. It is read in as a = (b = c), then b = c is evaluated first, and its return value is plugged into a = <that>. If the expression b = c didn’t return anything, you’d have a = <void> and the compiler would generate an error.
This is why a number of C library functions return a pointer to a buffer argument. For example, strcpy (that is, string copy), which takes in a destination memory address and a source address, returns the destination pointer when it’s done copying. This allows you to do something with the return instead of just sticking it by itself in a statement and putting a semicolon after it.
Combining expressions in this way can make your code a bit more difficult to read, which is probably why Java doesn’t support this style that well.
Someone who’s studied C++ Operator Overloading could tell you that the equals operator has similar to the following declaration:
int & operator=(int & left_hand_side, int & right_hand_side)
That is, the assignment operator returns a reference to the assigned value, allowing chaining (I think a better term) of assignments. As long as you assign a non-zero number, this statement will evaluate to TRUE. Definitely not the behavior you want…
Good for cheats, like assigning a NULL pointer within an IF statement would return FALSE, causing the block of code to be skipped. Good for experts, nightmare for beginners.
You said you’re used to working in Java but Java treats = as assignment and == as equality so I’m not sure what’s going on there.
As to cascading operators (a = b = c = 1), according to the ISO C standard that will not work. However, our compiler isn’t a strict C compiler. For instance, you can use C++ style comments (//comment) and it will work fine even though it’s not legal C. It’s not particularly important but is useful to know.
That sort of makes sense. After all, jdong shows how it can be represented with operator overloads and reference returns, yet C doesn’t allow reference types. Just shows how easy it is to take C++ for granted.
By the way, I remember reading somewhere that NULL (or any literal ‘0’, since NULL is a macro) is not guaranteed to evaluate to ‘false’ for all implementations. This means that you may not get the result you expect when you do “if(ptr)” to test if a pointer is safe to dereference. Or maybe I have it reversed, and it’s something related that isn’t guaranteed… Oh well, just be careful with your if conditions.
In C (and C++), a conditional false is any zero value. Any non-zero value is true. NULL is defined as a constant zero (the number, not the character). This means that, assuming your pointer has a value of null, if (ptr) will always work (I should note that it’s not particularly good style and you should use if ( ptr == NULL) instead).
One key thing to pointers is that they do not automatically become NULL when you free the memory allocated to them. You have to set that manually. So that may be what you’re referring to.