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
Related
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).
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)....
Apologize for ambiguous title but I simply did not know how else to put it.
Anyway, I want a piece of code to do the same thing this does in one line (in an if statement)
ret= fee(XYZ);
if((fii(ret) && foh(ret)) !=0)
{
//do something
}
like put all that into something like
if(_______FUM________)
{
//do the same thing
}
Is there anything I can do for this? The answer is probably staring at me in my face. But I am stuck here.
You could use the comma operator:
if(ret = fee(XYZ), (fii(ret) && foh(ret)) !=0)
But why?!
(Also, the !=0 appears to be redundant.)
if ((*l).proc == NULL)
{
(*l).proc = current_process;
if(current_process == NULL)
{
__no_operation();
}
if((*l).proc == NULL)
{
__no_operation();
}
}
When running this code, I added breakpoints at both no-ops. However, it only breaks at the second of the two. How is this possible?
Any sane compiler will have optimized these two identical blocks into a single block. It will probably also optimize the two conditionals into one. If you want to see both running separately, add puts("A"); to the first and puts("B"); to the second.