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)....
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);
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 have some functions where I would like to return NULL on failure, and on success they do not need to return anything. I have been using void * function_name(...), but it still requires me to return something.
The reason I am doing this is to detect failure as part of a python C extension and they recommend that the innermost function set any exception, return NULL and then do that up through the stack.
What is the best practice in this situation? Return 0, -1 or something similar?
In pure C terms, the easiest is to return a Boolean flag indicating success/failure.
As far as integration with Python is concerned, another approach is:
PyObject* func() {
...
if (error) {
/* set the exception */
PyErr_Format(PyExc_..., ...); /* TODO: fill in as appropriate */
return NULL;
}
/* return None */
Py_RETURN_NONE;
}
Many functions in C return 0 for success and !0 for failure; that's a good starting convention but at first can appear a little odd.
!0 can be enhanced to error codes at a later date (or some useful data like the return of strcmp yielding the position of the first difference); retaining 0 for OK.
I would suggest to return int, return 0 for success, return a negative value like -1 for failure, this follows many of the library functions.
The advantage is, there are usually multiple kinds of failure, you can return different int to represent them.
On the other hand, using the function in an if statement would seem a little confused.
As you are in C you can't use BOOLEAN true or false. But return 1 or 0 is fine.
But you can do this in that way :
int yourFunction( ... )
{
...
if ( success )
return 1;
return 0;
}
or even :
typedef int BOOL;
#define TRUE 1
#define FALSE 0
BOOL yourFunction()
{
if ( success )
return TRUE;
else
return FALSE;
}
or return -1 and 0 as it is suggested in another response.
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