0

Why do we use "==" instead of "=" to show equality in C++ codes????

Coding in C++, equality sign/ symbol

12th May 2019, 8:58 AM
Nigamananda Joshi
Nigamananda Joshi - avatar
3 Answers
+ 6
Because = has a different meaning. int a = 5; if(a == 7) { puts("a = 7"); } This will check if a equals 7 and if it does, it will print "a = 7". if(a = 7) { printf("a = %d\n", a); } This will set a to 7 and check if 7 evalutes to true. It does, so it'll print "a = 7". In 99% of all cases, you probably don't want to use an if clause to assign a new value to a variable, so a special symbol is needed to separate assignment (=) from equality checking (==).
12th May 2019, 9:13 AM
Anna
Anna - avatar
+ 3
Because "=" is reserved for different thing.
12th May 2019, 9:52 AM
Seb TheS
Seb TheS - avatar
+ 2
= is the assignment operator : int x = 2 ; //variable x is 2 == is the test operator : 2 == 2 //true That's the same in many langages.
12th May 2019, 9:15 AM
Théophile
Théophile - avatar