Yes, that's correct.
Originally Posted by sfortunato
As I said before, when you get to pointers (which will be denoted with variables, typically), they'll become a great help. You can plug anything in C into a boolean expression - it does no sort of type checking that I know of, only evaluates what's there.
Your example would work, yes, 5 && 0 == FALSE since it's evaluated as: TRUE && FALSE (which is FALSE), so negating that would be TRUE.
The reason it seems so weird is because you're not using them right, consider you have 3 status codes, 0, 5, and 10, and you want to print different things depending on which it is.
You will see boolean expressions a lot in loops and if statements, mainly to determine whether to do something.
int status_code = getStatusCode();
if (status_code == 5 || status_code == 10) // Is True if status code equals 5 OR if status code equals 10
else if (!status_code) // Is True if status code is equal to 0, as !0 = TRUE
...do something else
Let me give you a few more examples to help illustrate how boolean expressions are typically used
Let's say you need to count down..
Or if you need to poll input until there's no more (ie end of file)
int counter = 230; // arbitrary number
while (counter) // **(can also be: while (counter != 0 OR while (counter > 0), etc. - there will typically be more than one way to do it)**
counter = counter - 1;
And like I said, pointer validation. If the stdlib function: malloc (memory allocator) is unable to allocate the requested memory (not enough memory, for instance), it will return a NULL pointer - you can check for this.
while (fgets(...)) ... //** fgets() can read from an open file handle, it will return a NULL pointer upon end of file, which will evaluate to 0, and will then be FALSE, and will exit the loop
The multiple token boolean expressions, come into play when more conditions need to be met, ie.
void* my_pointer = malloc(128);
printf("Unable to allocate memory\n");
if (input_value < 0 || input_value > 31) // bounds checking, for instance
It is very common for functions in C to return a 0 if everything is working well, and an error code if it's not, these have to be negated with the exclamation mark or the equality operator.
if (!getErrorState() && offset < 100) // This can call that function (that I just made up now, would return a 0 if everything is going well, return an error code if it's not, this is the standard C way typically, that's why it needs to be negated. So it says: "if the error state is 0 (no error) AND the offset is less than 100, do this"
This is ironically contrary to pointer validation, in which 0 means it's NULL, and probably NOT working well, and a valid address that it's pointing to MAY mean that it is.
Programs typically exit with the status code 0 if all is well, that's where you get the 'return 0;' from at the end of main. exit(0); will do the same thing. The numbers - you define them, but 0 is generally accepted as "no error" by just about everything.
Unlike C++ and Java, C has NO built-in boolean, so boolean expressions must evaluate numeric values - which is everything, even characters as they're just numbers represented differently. Hope that cleared up better when you could use it.
When working in C world, remember what I said earlier:
If it's not 0, it's true