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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

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

bookComparison Operators

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 4
some-alt