# Lesson 3.1. Arithmetic operators

After studying the variables, we are now going to study the different types of operations, starting with the arithmetic operations (addition, multiplication, etc.).

## Vocabulary

First, let's clarify the vocabulary related to operators:

• Operator: symbol specifying the operation
• Operand: element(s) on which an operation is applied
• Unary operator: operator on one operand only (for example : absolute value, square root)
• Binary operator: operator on two operands (for example : addition, multiplication)

## Example 1

``x = a + b;``

In the example above, we add the variables `a` and `b` in order to put the result in `x`.

• `a` and `b` are the operands.
• The operator is the addition `+`.
• This is a binary operator.

## Example 2

``x = -a;``

In the above example, we calculate the opposite of the variable `a` in order to assign the result to `x`.

• `a` is the operand.
• The operator is the opposite `-` (or negation).
• This is a unary operator.

## Arithmetic operators

In C, there are 5 arithmetic operators:

Operator Symbol Type
Addition `+` unary or binary
Subtraction `-` unary or binary
Multiplication `*` binary
Division `/` binary
Modulo `%` binary

Note: The operators power, square root, exponential, sine, etc. are not native C operators. they have been implemented in the math.h library .

We will explain the modulo in the next lesson.

## Exercise 1

Write a program that declares 3 variables `a`, `b` and `x` (type `int`).

• The program initializes `a` and `b` to 5 and 78 respectively
• The program multiplies `a` by `b` and puts the result in `x`.
• Finally, the program displays the value of `x`.
``````#include <stdio.h>

int main(void) {
// Declare x, a=5 et b=78 (type int)
// COMPLETE HERE

// Multiply a by b => x
// COMPLETE HERE

// Display x
printf ("x = %d\n", x);

return 0;
}``````

## Exercise 2

Write a program that declares 5 variables of type `double` :

• `y` is not initialized
• `x1` having a value of 18
• `x2` having a value of 13
• `x3` having a value of 5
• `x4` having a value of 2

The program performs and displays the result of the following calculation:

\$\$ y = \dfrac {x1+x2} {x3-x4} \$\$

``````#include <stdio.h>

int main(void) {
// Declare the variables y, x1, x2, x3 et x4
// COMPLETE HERE

// Calculation
// COMPLETER ICI

// Display the result
printf ("y = %lf\n", y);

return 0;
}``````

## Exercise 3

Write a program that converts the angle `alpha` from degrees to radians (see formula).

``````// Angle to convert
double alpha = 90;

// Convert angle from degrees to radians
// COMPLETE HERE

// Displays the converted angle
printf ("alpha = %lf rad.\n", alpha);``````

For the value of π, you can use the constant `M_PI` declared in the `math.h` library.

## Quiz

In the following instruction, what are the operands?

``x = a - b;``

In the following statement, what are the operators ??

``x = a + b*c;``

What is a unary operator?

What is a binary operator?

What will the variable `x` contain after the following line?

``x = 2 + 3*5 - 1;``