6

What is the meaning of == and how does it differ from =?

How do I know which one to use?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Kraken
  • 23,393
  • 37
  • 102
  • 162
  • 4
    Comments deleted because they distracted attention from the answer. SO has a pretty clear policy of `No question is too trivial or too "newbie".` stated in the FAQ (http://stackoverflow.com/faq). If you want to discuss that policy, please take it up on http://meta.stackoverflow.com/ – Bill the Lizard Jan 28 '10 at 13:58

5 Answers5

21

== is a test for equality. = is an assignment.

Any good C book should cover this (fairly early on in the book I would imagine).

For example:

int i = 3;                       // sets i to 3.
if (i == 3) printf("i is 3\n");  // prints it.

Just watch out for the heinous:

if (i = 4) { }

which is valid C and frequently catches people out. This actually assigns 4 to the variable i and uses that as the truth value in the if statement. This leads a lot of people to use the uglier but safer:

if (4 == i) {}

which, if you accidentally use = instead of ==, is a compile-time error rather than something that will bite you on the backside while your program is running :-)

The logical-or operator is two vertical bar characters, one after the other, not a single character. Here it is lined up with a logical-and, and a variable called b4:

||
&&
b4

No magic there.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • http://www.cppreference.com/wiki/operator_precedence, http://www.cplusplus.com/doc/tutorial/. Check through these websites they were a real help when I was learning C++. – James Brooks Jan 28 '10 at 13:50
11

a == b is a test if a and b are equal.

a = b is called an assignment, which means to set the variable a to having the same value as b.

(You type | with Shift-\ in the US keyboard layout.)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
5

== tests equality = assigns a value

neither are related to ||

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
3

I might add that in Finnish and Swedish keyboards. Pipe symbol; |; of OR is AltGr (the right alt) and < key. IF you are using Mac on the other hand it is Alt-7 key.

Gave me a lot of sweat when I first started typing on these keyboards.

Rewolverine
  • 375
  • 1
  • 3
  • 8
2

Now that you know the difference between '==' and '=", let me put you some words of caution. Although '==' is used as a standard test of equality between comparable variables and '=' used as an internally type-casted assignment, the following programming error is quiet common.

In the below example and similar codes, '=' is know as "Always true" conditional operator.

#include<stdio.h>
int main()
{
    int i = 10, j = 20;
    if ( i = j )
        printf("Equal\n");
    else
        printf("NOT Equal\n");
    return 0;
}

So, the word of caution is "Never use '=' in if statements, unless you have something evil in your mind."

manav m-n
  • 11,136
  • 23
  • 74
  • 97
  • 3
    What? There's nothing quite so elegant as: if (fh = fopen("file.txt","r") { process_file(fh); close (fh); } // :-) – paxdiablo Jan 28 '10 at 14:12
  • I would also call it the "always true and bugger up i as a bonus" conditional operator. But good advice so +1. – paxdiablo Jan 28 '10 at 14:14
  • @paxdiablo: you are right... but i intended my word of caution for beginner's only. However, in goofy codes if i want a duplicate file descriptor as 'fh' this would lead to blunders: if (fd = fh = fopen("file.txt", "r") {//some code}; – manav m-n Jan 28 '10 at 14:30