0
Why do we use "==" instead of "=" to show equality in C++ codes????
Coding in C++, equality sign/ symbol
4 Antworten
+ 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 (==).
+ 3
Because "=" is reserved for different thing.
+ 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.