Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Comparison Operators | Operadores
Fundamentos de C

Comparison Operators

Deslize para mostrar o 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

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 >= 19 ); 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

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?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 3. Capítulo 4
some-alt