views:

314

answers:

9

Hi,

I am trying to figure out what == sign means in this program?

int main()
{
    int x = 2, y = 6, z = 6;        
    x = y == z;
    printf("%d", x);
}
+3  A: 

Equality. It returns 1 if the operands are equal, 0 otherwise.

Ignacio Vazquez-Abrams
+5  A: 

It is "equals" operator.

In the above example, x is assigned the result of equality test (y == z) expression. So, if y is equal to z, x will be set to 1 (true), otherwise 0 (false). Because C (pre-C99) does not have a boolean type, the expression evaluates to an integer.

Alex B
Downvote? I'd love an explanation.
Alex B
I didn't downvote, but off the top of my head C99 *does* have a boolean type via. `stdbool.h` - it's an actual compiler supported type, not just an `int` or `enum` (`(int) true == 1` still holds, though). Devs do not have to use it.
detly
@delty, probably should clarify that I mean C89.
Alex B
@detly: It's named `_Bool`. The name alone is probably enough to reduce excessive usage …
Christopher Creutzig
@Christopher Creutzig - the type name reserved for compiler use is `_Bool`, but every header I've ever seen has `#define bool _Bool` — I don't know if that's mandated by the standard though. Also, I discovered that my statement above is misleading — `true` is just a `#define` and does not have a compiler-recognised type.
detly
@detly: No, the standard defines the type `_Bool`, *as the type to be used by the user* (6.2.5.2). It also defines that `<stdbool.h>` defines the *macro* `bool` to expand to `_Bool` (which then is still the name appearing in error messages and warnings. Clause 7.1.3.1 makes `_Bool` a reserved identifier, but that does not mean a program should not use it – it means a program is not allowed to introduce a definition for this name itself. The goal is to have a name space for compiler writeres *and* future standard extensions. (The names `_Complex` and `_Imaginary` are meant for the user, too.)
Christopher Creutzig
Oh, and the standard explicitly says that undefining `bool`, `true`, and `false` is fine, while changing `_Bool` leads to undefined behavior.
Christopher Creutzig
+19  A: 

The == operator tests for equality. For example:

if ( a == b )
    dosomething();

And, in your example:

x = y == z;

x is true (1) if y is equal to z. If y is not equal to z, x is false (0).

A common mistake made by novice C programmers (and a typo made by some very experienced ones as well) is:

if ( a = b )
    dosomething();

In this case, b is assigned to a then evaluated as a boolean expression. Sometimes a programmer will do this deliberately but it's bad form. Another programmer reading the code won't know if it was done intentionally (rarely) or inadvertently (much more likely). A better construct would be:

if ( ((a = b) == 0 )   // or !=
    dosomething();

Here, b is assigned to a, then the result is compared with 0. The intent is clear. (Interestingly, I've worked with C# programmers who have never written pure C and couldn't tell you what this does.)

BillP3rd
+1 for not mentioning Yoda conditions.
dan04
For completeness, the value of `y == z` is either 1 or 0. So "x is true (non-zero)..." is better replaced with "`x` is 1 ...".
Alok
BillP3rd
A: 

== is the equality operator. If the arguments (y, z in your case) are the same, it will give 1, and 0 otherwise.

You can see a list of C operators here.

eli
A: 

It's saying

X will equal either true/1 or false/0.

another way to look at that line is this:

x =  ( is y equal to true? then true/1 or false/0 )
Pure.Krome
+2  A: 

== means "is euual to". This operator has higher precedece than = (equal to) operator. So the equation x = y == z; will try to assign result of y==z to variable x. which is 1 in this case.

Anil
Thanks for mentioning precedence. Another potential minefield for novice C programmers. +1
BillP3rd
A: 

== operator used for equality.. here in u r example if y is equal to z then x will hav true value otherwise x will hav false

Ricky Dang
A: 

Think about it like this:

= means give something a value.

== means check if it is equal to a value.

For example

int val = 5; //val is 5
//= actually changes val to 3
val = 3; 

//== Tests if val is 3 or not. 
//note: it DOES NOT CHANGE the value of val.
val == 3; 

int new_val = val == 3; //new_val will be 1, because the test is true

//the above statement is the same as
bool is_val_3 = false;
if( val == 3 )
   is_val_3 = true;
int new_val;
new_val = is_val_3;

//putting it together, 
val = new_val == 2; //sets val to 0. do you understand why now?
carleeto
+1  A: 
int main() 
{ 
    int x = 2, y = 6, z = 6;         
    x = y == z; 
    printf("%d", x); 
} 

let`s start like this:

 x = (6==6)

It asks is 6 equivalent to 6?: true

x = true, but since x is an int, x= 1 The new value of x is 1.

The following is printed:

1