Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

standards - double negation in C : is it guaranteed to return 0/1?

Is !!(x) guaranteed by the standard to return 0/1?

Note that I am not asking about c++, where a bool type is defined.

question from:https://stackoverflow.com/questions/8617090/double-negation-in-c-is-it-guaranteed-to-return-0-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, in C99, see §6.5.3.3/4:

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. The result has type int. The expression !E is equivalent to (0==E).

So !x and !!y can only yield 0 or 1, as ints.

For other operators, in C99, see also Is the "true" result of >, <, !, &&, || or == defined?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...