Originally Posted by
sfortunato
ah yes! awesome, i think i get it! so:
!( 1 || 0 && 1 )
goes to
!( 1 || 0 )
then
!(1)
then finally,
0
yes, am i right? thanks again so much for the help everyone.
also, don't mean to take advantage of the thread help here, but could you replace those numbers with variables? i.e.:
and if x was 5 and y was 0 it'd be true. or is this what you're supposed to do with these things? haha.
Yes, that's correct.
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.
ie.
Code:
int status_code = getStatusCode();
if (status_code == 5 || status_code == 10) // Is True if status code equals 5 OR if status code equals 10
...do something
else if (!status_code) // Is True if status code is equal to 0, as !0 = TRUE
...do something else
You will see boolean expressions a lot in loops and if statements, mainly to determine whether to do something.
EDIT:
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..
Code:
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)**
{
printf("%i\n", counter);
counter = counter - 1;
}
Or if you need to poll input until there's no more (ie end of file)
Code:
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
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.
Code:
void* my_pointer = malloc(128);
if (!my_pointer)
printf("Unable to allocate memory\n");
The multiple token boolean expressions, come into play when more conditions need to be met, ie.
Code:
if (input_value < 0 || input_value > 31) // bounds checking, for instance
or
Code:
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"
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.
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