Search⌘ K
AI Features

Building Expressions Using Operators

Explore how to construct expressions in C using various operators including arithmetic, relational, and logical types. Understand operator precedence and learn to write clear, error-free expressions with brackets. This lesson helps you confidently build complex expressions essential for effective C programming.

Like in any other programming language, in C, there are many arithmetic, relational, and logical operators. These operators can be used to create expressions that are made up of simpler basic types and constants. A C expression always evaluates to some value.

Arithmetic operators

The following binary arithmetic operators can be used in C: +, -, *, /, and %. The first four of these are for addition, subtraction, multiplication, and division. The last one (%) is the modulo operator, which, applied to two values, computes the remainder from dividing the first operand by the second operand:

C
#include<stdio.h>
int main(void) {
int a = 5, b = 2;
printf("The remainder from dividing a by b is %d\n", a % b );
return 0;
}

Precedence

When writing arithmetic expressions, we must always be aware of operator precedence, which is the order in which operators are applied when evaluating an expression.

For example, 4 + 5 * 6 evaluates to 34, because the * operator has precedence over the + operator, and so the expression is evaluated as 4 + (5 * 6), and not as ...