What is the meaning of '==' in C?
==
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.
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.)
== tests equality = assigns a value
neither are related to ||