Previous section To contents Next section

5.2 Comparison operators

The arithmetic operators would be hard to use for anything interesting without the ability to compare the results to each other. For this purpose there are six comparison operators:

Function Syntax Identifier Returns
Same a == b `== 1 if a is the same value as b, 0 otherwise
Not same a != b `!= 0 if a is the same value as b, 1 otherwise
Greater than a > b `>  1 if a is greater than b, 0 otherwise
Greater than or equal to a >= b `>= 1 if a is greater to or equal to b, 0 otherwise
Lesser than a < b `<  1 if a is lesser than b, 0 otherwise
Lesser than or equal to a <= b `<= 1 if a is lesser than or equal to b, 0 otherwise

The == and != operators can be used on any type. For two values to be same they must be the same type. Therefore 1 and 1.0 are not same. Also, for two values of pointer types to be the same the two values must be pointers to the same object. It is not enough that the two objects are of the same size and contain the same data.

The other operators in the table above can only be used with integers, floats and strings. If you compare an integer with a float, the int will be promoted to a float before the comparison. When comparing strings, lexical order is used and the values of the environment variables LC_CTYPE and LC_LANG are respected.


Previous section To contents Next section