Was working with some SASL code today and noticed the == in the below snippet. I'm no C expert but the only way I've ever used that operator was to test equality. Bug?
if ( !conn ) {
rc == LDAP_SUCCESS;
goto done;
}
That statement does nothing. It's a bug.
Now, you COULD assign (rc == LDAP_SUCCESS) to a variable, which would store the boolean result of that operation (1 if true, or 0 if false).
Related
I'm seeing in one of the files I've inherited the following line
while ((status = SOME_STATUS == FunctionName(params)))
Obviously names have been changed, but you get the idea. Can someone explain to me how the compiler sets the values and in what order...
I'm thinking that status get set to SOME_STATUS and then is set the result of the function?
I've never seen this in all my years developing. Why in the world would someone do this? It's completely nuts... or maybe I am!
Thanks.
-stv
Personally I love code like this since once you've memorised your operator precedence table, it's extremely clear.
= has lower precedence than ==, that's all.
If you insert the superfluous parentheses it's obvious that status is 1 or 0 acccording to the test of relational equality.
The double opening parentheses probably suppress a compiler warning.
The expression SOME_STATUS==Func(Params) is a boolean expression resolving to true or false, thus while( status = <boolean expression>) means:
Assign variable status with true (SOME_STATUS == Func(Params)) or false (SOME_STATUS != Func(Params))
Continue with loop untill status is false (i.e. -
SOME_STATUS != Func(Params))
I must admit that I prefer a more readable code:
...
if( STATUS_OK != Func( Params))
bContinue = false; // or break;
} while( bContinue);
I have this chunk of code:
if ( ( data.is_err != FALSE ) && ( available != FALSE ) )
{
Set_Dtc_TimerChange(DTC_VPOS, +dt_ms);
}
Why would you use not equal to false instead of just equal to true? Is there a difference at all?
This is a C question rather than an EE, but as C is very close to the hardware....
The old C definition of false is 0, with all other values being true, that is
if( 0 ){ ... }
will not execute the ... code, but for instance
if( 1 ){ ... }
if( -1 ){ ... }
if( 42 ){ ... }
all will. Hence you can test a truth value for being equal to FALSE (which is defined as 0), but when you want to compare it to true, which value would you use for true? Some say TRUE must be defined as (!FALSE), but that would yield -1, which is a value of true, but not the only one.
Hence programmers tended to avoid conparing to TRUE.
Modern C should have solved this by defining TRUE to be (I think) 1, but that stil has funny consequences, like
int probably( void ){ return 42; }
if( probably() ){ ... }
if( probably() == TRUE ){ ... }
Here the blame is on probbaly(), which returns an abiguous truth value.
Personally, I would have written
if ( data.is_err && available ) { ... }
which I think is much more readable and avoids the comparisons.
(BTW how can something be available when there is a data error?)
This code can be safely replaced by:
if ( data.is_err && available ) {
Set_Dtc_TimerChange(DTC_VPOS, +dt_ms);
}
unless the FALSE has some non-traditional definition somewhere in the program. A good compiler will produce equivalent code for either form, but the more readable one is preferred.
C does not have a Boolean type. Instead, it treats 0 as false and any other value as true. As a result, there is no value that you can test against to determine whether a value represents true. That's not important if you write idiomatic C code: if (x) ... will always do the right thing. Folks who are uncomfortable with this idiom like to test explicitly against some representation of false, but never explain why they limit that to one level. After all, x != FALSE is just a value, and by the same argument needs to be tested: if ((x != FALSE) != FALSE)....
if(tree->left)
if(!(*tree))
Do they mean:
if(tree->left==0)
if((*tree)==1)
I did not find anything about this.
In C there are not booleans but only integers. The if statement just check for equality with 0, evaluating 0 as false and everything else as true, so your examples are equivalent to
if(tree->left != 0)
if((*tree)==0)
It means
if(tree->left != 0 )
if((*tree) == 0)
C and C++ will interpret 0 as false and anything else as true.
For example, doing
if (my_var & 0x0010) {
printf("Hello world");
}
will print the message if my_var has the bit set in the fifth position.
I am writing code in embedded c(in Kiel4) comparing String in buffer AtRes with C003 and it will assigned to uc EVENTbuf like
else if(ucEVENTBuf=tmMisc_strnstr((INT8C *)AtRes,"C003,",4))
now I am getting warning
assignment in condition
how to overcome thise warnning.
Thank you
Your compiler is interprething the if statemente as follows:
if( ucEVENTBuf=1 ) // or whatever
which is an assignement. As other people said it is usually sufficient to put other parentesis around:
if( (ucEVENTBuf=1) ) // or whatever
but if also this doesn't work for you, you may try this:
else if((ucEVENTBuf=tmMisc_strnstr((INT8C *)AtRes,"C003,",4))!=0) /* <<<<< ask explicitely to compare against 0 */
A common error when programming C in the old days was to accidentally use assignment instead of comparison:
if (x = 3)
So compilers started warning about this. But if you really mean to assign and use the result as a boolean expression, you can. Some compilers will not warn if you add parentheses:
if ((x = 3))
If that isn't enough on your system, you can try to make it more explicit:
if (!!(x = 3))
Try to replace
else if(ucEVENTBuf=tmMisc_strnstr((INT8C *)AtRes,"C003,",4))
with
else if((ucEVENTBuf=tmMisc_strnstr((INT8C *)AtRes,"C003,",4)))
Or with
else if((ucEVENTBuf=tmMisc_strnstr((INT8C *)AtRes,"C003,",4)) != NULL)
If your compiler still gives you the same warning (it should not), then try this
else {
ucEVENTBuf=tmMisc_strnstr((INT8C *)AtRes,"C003,",4);
if (ucEVENTBuf != NULL) {
}
}
You may need do other changes to the structure of your code.
I have the following code which functions correctly.
if((strcmppgm2ram((char*)name, (ROM char*)"products.htm") != 0))
{
if((strcmppgm2ram((char*)name, (ROM char*)"restock.htm") != 0))
{
return HTTP_IO_DONE;
}
}
I'd like to clean it up and put it in the form:
if((strcmppgm2ram((char*)name, (ROM char*)"products.htm") != 0) || (strcmppgm2ram((char*)name, (ROM char*)"restock.htm") !=0))
{
return HTTP_IO_DONE;
}
Unfortunately, the latter is not working correctly. What have I overlooked?? Thanks in advance!
P.S. strcmp == strcmppgm2ram for the sake of this question.
You are using || when you should be using &&.
The result should only be reached if BOTH conditions are true.
The reason it didn't work before is because even if one of the strings matched, the other one wouldn't, so regardless of input the statement would always result in returning HTTP_IO_DONE.
Notice that you only return HTTP_IO_DONE; if both conditions turned to be true, therefore you should use and (&&) and not or