Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the meaning of == sign?

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);
}
like image 777
bunty Avatar asked Sep 02 '10 05:09

bunty


People also ask

What does == mean?

"equal to", for example, this statement if x == 7: Means "If variable X is equal to seven"

What does == mean in text?

That is the 'equal to' sign sign. It's called an 'comparison operator'. e..g. text.length == text.length OR text.length == 4 OR 5 + 10 == 15.

What does two == mean?

Just keep it all straight by remembering that only the double equal sign means “is equal to” and the single equal sign can be roughly translated into “is.”

What is == used for?

The equality operator ( == ) checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, it attempts to convert and compare operands that are of different types.


1 Answers

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.)

like image 63
BillP3rd Avatar answered Oct 05 '22 03:10

BillP3rd