1? Although I myself would not write it, what is the expected result of the following statement, wh...">

C: What will be the statement "A" 1 'for A> 1?

Although I myself would not write it, what is the expected result of the following statement, where A(guaranteed to be zero or a positive integer) is greater than 1?

return A || 1;

In many languages, I would expect it to Abe returned if the value Ais zero, in which case 1 will be.

I do not have my C book, but I note that the value 1 is actually always returned. Is this the result of compiler optimization or the potential ambiguity of the expression, is it that the return value is non-deterministic?

+3
source share
6 answers

The standard says

|| 1, 0; 0. int.

. 6.5.14 .

+18

: YES ( true)

|| , (2- , , )

+3

C ( Objective-C). 1.

+2

C ||, && ! 0 1, , . , A || 1 1.

0

C ( 1). A , , OR

, , A, 1, || 2. 2 , OR, true ( 1)

0

, A. - ? , A & 0, A || 1 A || 0 (= A & 1) , , 0, 1 .

0

Source: https://habr.com/ru/post/1758955/


All Articles