Embedded Development Board Learning


Logical operators in C

Date: agosto 25, 2025

Author: Guillermo Garcia

Categories: Embedded C programming Tags:

In this article we are going to see the logical operators that are important for creating programs in C language.

Arithmetic expressions

In most C programs, one or more calculations will be needed. To this end, the C language is equipped with a number of arithmetic operators as summarized.

operation and operator table in C

Some remarks:

  • the division of two integer numbers yields an integer number:
    example. 1 / 2 = 0 but 1.0 / 2 = 0.5
  • the remainder operand % yields the remainder after integer division:
    example. 5 % 3 = 2
  • As such it can only be used with integer numbers.
  • The increment (decrement) operand increments (decrements) the content of the variable with 1. Writing ++ (–) before or after the variable yields different results:

Difference between i++ and ++i

What is the output of the following i++ code?

i=1;
j=1;
j=i++

output:
i=2;
j=1;

Use the current value of i in the expression (j=i), afterwards increment i by 1.

What is the output of the following ++i code?

i=1;
j=1;
j=++i;

output:
i=2;
j=2;

First increment i by 1, then use the new value of i in the expression (j=i).

Conditional expressions

This evaluate to one of following results:

  • true (every value different from 0 is seen as true)
  • false (0)

Conditional expressions are formed by using the equality operators, the relational operators and the logical operators as summarized.

Logical operators table in C

The symbols in the operators ‘==’, ‘!=’, ‘<=’ and ‘>=’ cannot be separated by one or more spaces!.

Often the equality operator ‘==’ is confused with the assignment operator ‘=’ . The expression ‘x=5’ is always true since the assignment operator takes care of assigning the value 5 to the variable x which is not equal to 0. The expression ‘x==5’ is true only if the variable x contains the value 5.

Precedence

If an expression contains more than one operator, C will apply these operators in a sequence that is determined by the rules of precedence as summarized. The first ones in the table have higher priority.

Precedence operators in C

Assignment statements

To change the value stored in a variable, an assignment statement is used. In its most general form, the assignment statement can be written as:

<variable> = <expression>
example
sum = sum + number;
y=2 * cos(x) + 4;

The expression on the right-hand side of this statement will first be evaluated into a result. Afterwards, this result will be assigned to the variable written on the left-hand side of the assignment operator.

Arithmetic assignment operators

C provides a number of assignment operators for abbreviating assignment expressions. For example the statement x = x + a; can be abbreviated with the addition assignment operator ‘+=’ as.

Aritmetic operators in C

Summary

Summary operator in C


Card image cap
Guillermo Garcia Thanks for reading! I am one of the editors of this site and I am committed to providing you with the best information possible by sharing my experience in embedded systems.


Comments... There are no comments.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Publicidad