Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value of "==" operator in C

Tags:

c

Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?

struct ss {     int id; };  struct os {     int sid;     int state; };  int count(struct ss *s, int state) {     int num = 0;     // foreach o (of type os*) in a hash table         num += o->state == state && (s ? o->sid == s->id : 1);      return num; } 

So o->sid == s->id will return always 1 or 0, or it can return other values?

like image 731
Victor Dodon Avatar asked Aug 07 '13 08:08

Victor Dodon


People also ask

What are the possible values of == operator?

This means that each bit can be one of 4 values: 0,1,x,z. With the "case equality" operator, === , x's are compared, and the result is 1. With == , the result of the comparison is not 0, as you stated; rather, the result is x, according to the IEEE Std (1800-2009), section 11.4.

What is === operator in C?

=== is not an operator in C. It is used in other languages, such as PHP. In PHP, === checks for value and type. – Sarwar Erfan.

What does the operator return in C?

The assignment operators in C and C++ return the value of the variable being assigned to, i.e., their left operand. In your example of a = b , the value of this entire expression is the value that is assigned to a (which is the value of b converted into the type of a ).

What does a comparison return in C?

In the C Programming Language, the strcmp function returns a negative, zero, or positive integer depending on whether the object pointed to by s1 is less than, equal to, or greater than the object pointed to by s2.


1 Answers

Can I assume that in C, the "==" operator will always evaluate to 1 if the two values are equal or it can evaluate to other "true" values?

Yes, and so does != > < >= <= all the relational operator.

C11(ISO/IEC 9899:201x) §6.5.8 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.107) The result has type int.

like image 81
Yu Hao Avatar answered Sep 19 '22 17:09

Yu Hao