Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relational operators evaluation result

Assume we have an expression like

(x > 5) 

in C language. Is there any guarantee given by the language / standard that the expression will be evaluated to 0 when it's false and to 1 when it's true?

like image 247
Tomek Avatar asked Feb 16 '15 09:02

Tomek


1 Answers

Yes, it is guaranteed by the standard.

As per the C11 standard document, chapter 6.5.8, paragraph 6, [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. The result has type int.

Update: Same chapter and paragraph for C99 standard also.

like image 55
Sourav Ghosh Avatar answered Oct 16 '22 10:10

Sourav Ghosh