Assignment, Comparison, Not Equal To
You're already familiar with this operator. It assigns the value on the right to the variable on the left.
main.c
1234567#include <stdio.h> int main() { int x = 5; // Assigns the value 5 to variable `x` int y = 8; // Assigns the value 8 to variable `y` x = y; // Assigns the value of `y` to `x` (so now, `x` is 8) }
The assignement operator can be easily mistaken for the comparison one, but they do different things: = assigns; == compares and returns true/false. In conditions use == because = would modify the variable and the expression becomes that value.
Main.c
123456789#include <stdio.h> int main() { int result = (50 == 2); printf("%d", result); return 0; }
The expression (50 == 2) is false, or 0, because 50 is not equal to 2. You can see this by running the code yourself.
The binary values 0 and 1 can also represent states and can be use instead of true or false.
For inequality, you can use !=, which is true when the values differ. For example, 50 != 2 is true. In C, booleans are integers, so true is 1 and false is 0, meaning that expression evaluates to 1.
Main.c
123456789#include <stdio.h> int main() { int result = (50 != 2); printf("%d", result); return 0; }
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 2.63
Assignment, Comparison, Not Equal To
Sveip for å vise menyen
You're already familiar with this operator. It assigns the value on the right to the variable on the left.
main.c
1234567#include <stdio.h> int main() { int x = 5; // Assigns the value 5 to variable `x` int y = 8; // Assigns the value 8 to variable `y` x = y; // Assigns the value of `y` to `x` (so now, `x` is 8) }
The assignement operator can be easily mistaken for the comparison one, but they do different things: = assigns; == compares and returns true/false. In conditions use == because = would modify the variable and the expression becomes that value.
Main.c
123456789#include <stdio.h> int main() { int result = (50 == 2); printf("%d", result); return 0; }
The expression (50 == 2) is false, or 0, because 50 is not equal to 2. You can see this by running the code yourself.
The binary values 0 and 1 can also represent states and can be use instead of true or false.
For inequality, you can use !=, which is true when the values differ. For example, 50 != 2 is true. In C, booleans are integers, so true is 1 and false is 0, meaning that expression evaluates to 1.
Main.c
123456789#include <stdio.h> int main() { int result = (50 != 2); printf("%d", result); return 0; }
Takk for tilbakemeldingene dine!