C Operators Tutorial

In this C tutorial we learn about operands, lvalues and rvalues. We also learn about the arithmetic, assignment, relational, logical, unary and ternary operators.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

What is an operator

An operator is one or more symbols that have a special meaning, in certain contexts, to the compiler.

For example, the + symbol is the addition operator and will perform addition arithmetic on two numeric values.

The following types of operators are available in C.

  • Arithmetic operators
  • Assignment operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Increment/Decrement operator
  • Ternary conditional operator
  • Other operators

Operands, Lvalue & Rvalue

There are two kinds of expressions in C, Lvalues and Rvalues. Lvalues and Rvalues are also known as the left operand and the right operand respectively, or operands in general.

An lvalue is the operand that can have a value assigned to it. It’s allowed to appear on either the left or the right side of an assignment.

Example:
// lvalue (num1) may be
// on the left of the =
int num1 = 10;

// lvalue may also be on
// the right of the =
int num2 = num1;

An rvalue is the operand that cannot be assigned a value. It may only appear on the right side of an assignment.

Example:
// rvalue (10) may only
// be on the right of the =
int num1 = 10;

// an rvalue cannot be
// on the left of the =
10 = 10;
Arithemtic operators

Arithmetic operators allow basic mathematical arithmetic to be performed on operands.

The following table lists the arithmetic operators available in C.

OperatorDescription
+Add the right operand to the left operand.
*Multiply the left operand with the right operand.
/Divide the left operand by the right operand.
%Modulo. Divide the left operand by the right operand and return the remainder of the division.
Assignment operators

An assignment operator is used to assign the operand on the right, to the operand on the left.

The following table lists the assignment operators available in C.

OperatorDescriptionExampleSame as
=Assigna = ba = b
+=Add and assigna += ba = a + b
-=Subtract and assigna -= ba = a - b
*=Multiply and assigna *= ba = a* b
/=Divide and assigna /= ba = a / b
%=Modulo and assigna %= ba = a % b
Relational operators

Relational operators evaluate the relationship between operands and are primarily used in conditional and iteration control flow.

The following table lists the relational operators available in C.

OperatorDescriptionExample
==Equal to1 == 1 is true
>Greater than2 > 1 is true
<Less than3 < 1 is false
!=Not equal to2 != 3 is true
>=Greater than or equal to2 >= 1 is true
<=Less than or equal to5 <= 5 is true
Logical operators

Logical operators are used to combine conditional evaluations. The result is either true or false.

The following table lists the logical operators available in C.

OperatorNameDescription
&&AndReturns true if both expressions evaluate to true.
||OrReturns true if one or the other expression evaluate to true.
!NotReturns true if the expression immediately following it not true.
Increment / Decrement (unary) operators

Incremental operators are used to increase a value incrementally, and decremental operators are used to decrease a value.

These operators are what is known as unary operators and as such only require one operand.

The following table lists incremental and decremental operators available in C.

OperatorNameDescription
++aPre-incrementIncrement by one, then return __a__.
a++Post-incrementReturn __a__, then increment by one.
--aPre-decrementDecrement by one, then return __a__.
a--Post-decrementReturn __a__, then decrement by one.
The ternary operators

The ternary operator is a conditional operator that functions as a replacement for a conditional if/else expression with only single execution statements.

OperatorDescription
? :Replaces a simple conditional if/else statement.
Example:
int num = 10;

if (num > 5)
{
	printf("Num > 5");
}
else
{
	printf("Num < 5");
}

The normal if/else statement in the example above, will become the following expression when converted to a ternary.

Example:
int num = 10;

// condition	if true				if false
num > 5 	?	printf("Num > 5") : printf("Num < 5");

We look at the ternary operator again in the lesson on conditional control with if/else statements.

Other operators

C supports more operators in addition to all of the operators mentioned above.

OperatorNameDescription
.Dot notationAccess the members of structures and unions.
->Arrow notationAccess the members of structures and unions.
*DereferenceDereference the value of a pointer variable.
&Address ofGet the actual memory address of a variable.
sizeof()Size of operatorGet the size of a data type.

A blank space is considered to be the separator operator. It separates programming elements from each other.

As an example, a separator will separate a keyword from another keyword, or a keyword from an identifier.

Summary: Points to remember

  • Operators are special symbols in C that allow us to perform operations.
  • Operands are the lvalue and rvalue expressions in C.