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

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)

`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.

`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.

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.

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;
}
```

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;
}
```

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.

**In the following instruction, what are the operands?**

`x = a - b;`

Check
Bravo! Operands are the elements on which an operation is applied.
Try again...

**In the following statement, what are the operators ?**?

`x = a + b*c;`

Check
Bravo! There are two operators: multiplication and addition.
Try again...

**What is a unary operator?**

Check
Bravo! A unary operator accepts only one operand.
Try again...

**What is a binary operator?**

Check
Bravo! A binary operator applies to two operands.
Try again...

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

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

Check
Bravo! Multiplication is a priority.
Try again...

- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 11/21/2022