Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Assignment, Comparison, Not Equal To | Operators
C Basics

bookAssignment, 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

main.c

copy
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

Main.c

copy
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.

Note
Note

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

Main.c

copy
123456789
#include <stdio.h> int main() { int result = (50 != 2); printf("%d", result); return 0; }
question mark

What is the output of the next code?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 2.63

bookAssignment, Comparison, Not Equal To

Stryg for at vise menuen

You're already familiar with this operator. It assigns the value on the right to the variable on the left.

main.c

main.c

copy
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

Main.c

copy
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.

Note
Note

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

Main.c

copy
123456789
#include <stdio.h> int main() { int result = (50 != 2); printf("%d", result); return 0; }
question mark

What is the output of the next code?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1
some-alt