Can you compare the results of Boolean operations?

Is the C standard means to ensure that the logical operations ( ==, !=, >, &&, ||, etc.) always have the same meaning for the submission of truthfulness? In other words, do they always return some positive constant, if true, or is this the only guarantee that it will be a positive number?

The reason I ask for this is to know if statements like the ones below are. This expression must be true if both pointers are NULL or both pointers point somewhere (not necessarily the same thing).

if ((ptr1 == NULL) == (ptr2 == NULL)) 
+3
source share
3 answers

, C , 0 , (a) 1 0.

, (a == b) 42, .

(C11) 6.5 Expressions:

6.5.8/6: 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.

6.5.9/3: The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false.

6.5.13/3: The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0.

6.5.14/3: The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0.

, . , ( ), - NOT !, :

6.5.3.3/5: The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0.


(a): . C11, if, while, do for, -, /while "the expression compares unequal to zero". :

6.8.4.1/2: In both forms [of the if statement, one with and one without an else clause], the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0.

6.8.5/4: An iteration statement [while, do and for] causes a statement called the loop body to be executed repeatedly until the controlling expression compares equal to 0.

+7

.

, , 1 true 0 false.

+1

results

  • negation operator ( !)

  • Relational Operators ( <, >, <=, >=)

  • Equality Operators ( ==, !=)

    logical operators ( &&, ||)

is inteither 0(false) or 1(true).

0
source

All Articles