...

/

Building Expressions Using Operators

Building Expressions Using Operators

Learn how about arithmetic, relational, and logical operators and how they can be used for building C expressions.

Like in any other programming language, in C, there are many arithmetic, relational, and logical operators that 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:

Press + to interact
#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 ...