Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Comparison Operators | Operators
C Basics

bookComparison Operators

Understanding Comparison Operators

Comparison operators let you evaluate and compare values. One of the trickier aspects of these operators is remembering the correct order or arrangement of the symbols, like determining whether < or = should come first.

When these operators are used in a program, the outcome will be either true or false. In the context of programming, true is typically represented as 1, and false is represented as 0.

Main.c

Main.c

copy
1234567891011
#include <stdio.h> int main() { printf("Expression 8 == 7 + 1 is %d\n", 8 == 7 + 1 ); printf("Expression 10 != 3 is %d\n", 10 != 3 ); printf("Expression 7 > 7 is %d\n", 7 > 7 ); printf("Expression 20 >= 19 is %d\n", 20 >= 20 ); printf("Expression 21 <= 21 is %d\n", 20 <= 21 ); return 0; }

You'll frequently see comparison operators in loops and conditional statements.

Operator Precedence

Grasping the order of operations, or operator precedence, is crucial.

When it comes to precedence, the increment (++) and decrement (--) operators are evaluated first. This is followed by the multiplication (*) and division (/) operators. Lastly, the addition (+) and subtraction (-) operators are evaluated.

main.c

main.c

copy
123456789101112
#include <stdio.h> int main() { int a = 5; int b = 3; int c = 7; int d = a * ++b * c-- + 4; printf("The value of d: %d", d); return 0; }

To make complex expressions easier to read, you can add parentheses to show grouping and operator precedence.

int d = ((a * (++b)) * (c--)) + 4;

Prefix increment applies before use, postfix decrement after evaluation, and multiplications precede + 4. In the end the d variable will be 144 (and afterward b = 4, c = 6).

question mark

Which precedence order is correct?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain more about how operator precedence works with comparison operators?

What happens if I use multiple comparison operators in a single expression?

Can you give more examples of using comparison operators in conditional statements?

Awesome!

Completion rate improved to 2.63

bookComparison Operators

Glissez pour afficher le menu

Understanding Comparison Operators

Comparison operators let you evaluate and compare values. One of the trickier aspects of these operators is remembering the correct order or arrangement of the symbols, like determining whether < or = should come first.

When these operators are used in a program, the outcome will be either true or false. In the context of programming, true is typically represented as 1, and false is represented as 0.

Main.c

Main.c

copy
1234567891011
#include <stdio.h> int main() { printf("Expression 8 == 7 + 1 is %d\n", 8 == 7 + 1 ); printf("Expression 10 != 3 is %d\n", 10 != 3 ); printf("Expression 7 > 7 is %d\n", 7 > 7 ); printf("Expression 20 >= 19 is %d\n", 20 >= 20 ); printf("Expression 21 <= 21 is %d\n", 20 <= 21 ); return 0; }

You'll frequently see comparison operators in loops and conditional statements.

Operator Precedence

Grasping the order of operations, or operator precedence, is crucial.

When it comes to precedence, the increment (++) and decrement (--) operators are evaluated first. This is followed by the multiplication (*) and division (/) operators. Lastly, the addition (+) and subtraction (-) operators are evaluated.

main.c

main.c

copy
123456789101112
#include <stdio.h> int main() { int a = 5; int b = 3; int c = 7; int d = a * ++b * c-- + 4; printf("The value of d: %d", d); return 0; }

To make complex expressions easier to read, you can add parentheses to show grouping and operator precedence.

int d = ((a * (++b)) * (c--)) + 4;

Prefix increment applies before use, postfix decrement after evaluation, and multiplications precede + 4. In the end the d variable will be 144 (and afterward b = 4, c = 6).

question mark

Which precedence order is correct?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4
some-alt