Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain more about how booleans work in C?

What other comparison operators are there in C?

Can you give more examples of using `==` and `!=` in conditions?

Awesome!

Completion rate improved to 2.63

bookAssignment, Comparison, Not Equal To

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1
some-alt