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

bookAssignment, Comparison, Not Equal To

メニューを表示するにはスワイプしてください

We've learned how to store data in variables, organize data into arrays, and display data as strings. Yet, we haven't explored how to modify or manipulate that data.

Assignment Operator (=)

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

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)

Comparison Operators (!=, ==)

Let's delve into how these work:

int result = (50 != 2);

The expression (50 != 2) evaluates to either true (1) or false (0), depending on the validity of the comparison.

The != operator stands for "not equal".

If 50 isn't equal to 2, then (50 != 2) evaluates to true.

In computing, the notions of "true" and "false" are numerically represented as 1 (true) and 0 (false):

  • 1 represents true;
  • 0 represents false.

Note

The binary values 0 and 1 can also represent states. We've already encountered these values when discussing byte states in a previous lesson.

For instance, with the != operator:

Main.c

Main.c

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

The expression (50 != 2) evaluates to true, or 1.

The == operator checks for equality.

For instance:

Main.c

Main.c

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

question mark

What is the output of the next code?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  1

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  1
some-alt