Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will a C conditional always return 1 or 0?

Do C conditional statements always return [1 or 0], or do they return [0 or 'something other than zero']. I ask because:

pseudo code -

foo(address, shouldSend):
register >>= 1
register <<= 1 // to clear bit in first position
register |= shouldSend // to signal whether it should send or not

the problem occurs if somebody passin IN a shouldSend value of true greater than one (as only 0 is false and all else is true, technically this is valid). since i am directly OR'ing the truth value of shouldSend with the register, it better not be say 0xFF! i already have a solution, so the question is more for curiousity sake. i am wondering though if:

foo(address, shouldSend):
register >>= 1
register <<= 1 // to clear bit in first position
register |= (shouldSend > 0) // to signal whether it should send or not

solves the problem? i would think that now the problem of an 0xFF(or in general, something greater than 1) passed in is masked by the C conditional. but this only holds IF C conditionals are guaranteed to return [0 or 1].

ps - i also realize that it is probably compiler dependent, but what does the ansi standard say about this?

like image 443
trh178 Avatar asked Aug 20 '10 15:08

trh178


People also ask

Is not 0 true in C?

Boolean Variables and Data Type ( or lack thereof in C ) For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.

What is conditional statement in C?

C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

Can we use return in conditional statement?

The return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements.

How to say or in if statement C?

If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) evaluates to 1. (0) OR (0) evaluates to 0. The OR is written as || in C.


2 Answers

Standard specifies that the result is always integer value equals to 0 or 1

6.5.8 Relational operators

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.92) The result has type int.

like image 115
Vladimir Avatar answered Nov 13 '22 07:11

Vladimir


Doesn't matter if it is specified or not. It is best to always test against false and be explicit about your or-equals values. This removes any worry about compiler implementations and is clearer and more maintainable.

like image 39
Ken Avatar answered Nov 13 '22 08:11

Ken