What is the difference between = and == in C?
= is the assignment operator. It stores a value. == is the equality operator. It compares two values and gives 1 or 0.
#include <stdio.h>
int main(void) {
int x = 5;
if (x == 10) printf("equal\n"); // false, nothing printed
if (x = 10) printf("assigned\n"); // assigns 10, always true
printf("x = %d\n", x);
return 0;
}
Output:
Output of the above program is
assigned
x = 10
The second if changed x to 10 by mistake. See C Operators.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |