Programming in Practical Engineering
Languages
Chapter 2 : Tools of the algorithm in C language
BURKINA INSTITUTE OF TECHNOLOGY
Electrical Engineering
(E.E)
Academic year : 2024-2025
Semester 3
11 octobre 2024
Course outline
1 The notions of variable and types of data
2 The constants defined by #define and const
3 Operators and expression
4 Instructions and control structures
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 2 / 50
Quote
A programming language is a convention for giving commands to a
computer. It’s not supposed to be obscure, weird and full of
subtle traps...
Dave Small
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 3 / 50
1. The notions of variable and types
of data
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 4 / 50
1. The notions of variable and types of data
1.1. The notions of variable
Variables are containers for storing data values.
It takes on a defined number of different values over the
course of the programme.
The type can be numbers and characters, etc.
There are different types of variables:
int - stores integers without decimals.
float - stores floating point numbers, with decimals.
char - stores single characters, such as ’a’ or ’B’
surrounded by single quotes
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 5 / 50
1.The notions of variable and types of data
1.1.2. Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Syntax :
type a variableName;
variableName = value;
type variableName = value;
Where type is one of C types (such as int), and variableName
is the name of the variable. The equal sign is used to assign
a value to the variable.
a. Data types will be defined in next section
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 6 / 50
1. The notions of variable and types of data
1.1.3. Examples
1 # include < stdio .h >
2 int main () {
3 // Declare a variable
4 int myNum ;
5
6 // Assign a value to the variable
7 myNum = 15;
8
9 // Declare and assign the value 15 to the variable :
10 int mynum = 15;
11 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 7 / 50
1. The notions of variable and types of data
1.2. Types of data
Data types are the type of data stored in a C program.
Data types are used while defining a variable or functions
in.
The compiler needs to understand the type of predefined data
it will encounter in the program.
A data type is an attribute that tells a computer how to
interpret the value.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 8 / 50
1. The notions of variable and types of data
1.2. Types of data
Most modern computer languages recognize five basic
categories of data types :
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 9 / 50
1. The notions of variable and types of data
1.2.1. Size Of Data Types In C
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 10 / 50
1. The notions of variable and types of data
1.2.2. printf() function and Types data
A variable in C must be a specified data type, and you must
use a format specifier inside the printf() function to
display it:
1 # include < stdio .h >
2 int main () {
3 // Create variables
4 int myNum = 5; // Integer ( whole number )
5 float myFloatNum = 5.99; // Floating point number
6 char myLetter = ’D ’; // Character
7
8 // Print variables
9 printf ( " % d \ n " , myNum ) ;
10 printf ( " % f \ n " , myFloatNum ) ;
11 printf ( " % c \ n " , myLetter ) ;
12 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 11 / 50
2. The constants defined by #define
and const
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 12 / 50
2. The constants defined by #define and const
2.1. Introduction
Constants : If you don’t want others to change existing
variable values, you can use the const or #define keywords.
This will declare the variable as constant, which means
unchangeable and read-only :
Example :
const int myNum = 15 ;
myNum will always be 15
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 13 / 50
2. The constants defined by #define and const
2.2.1. Const keyword
We define a constant in C language using the const keyword.
Also known as a const type qualifier, the const keyword is
placed at the start of the variable declaration to declare
that variable as a constant.
Syntax:
const data_type var_name = value;
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 14 / 50
2. The constants defined by #define and const
2.2.2. Const: example
1 # include < stdio .h >
2
3 int main () {
4
5 // defining integer constant using const keyword
6 const int int_const = 25;
7
8 // defining character constant using const keyword
9 const char char_const = ’A ’;
10
11 // defining float constant using const keyword
12 const float float_const = 15.66;
13
14 printf ( " Value of Integer Constant : % d \ n " , int_const ) ;
15 printf ( " Value of Character Constant : % c \ n " , char_const ) ;
16 printf ( " Value of Float Constant : % f " , float_const ) ;
17 return 0;
18 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 15 / 50
2. The constants defined by #define and const
2.3.1. #define keyword
We can also define a constant in C using #define
preprocessor.
The constants defined using #define are macros that behave
like a constant.
These constants are not handled by the compiler, they are
handled by the preprocessor and are replaced by their value
before compilation.
Syntax:
#define var_name value;
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 16 / 50
2. The constants defined by #define and const
2.3.2. #define: example
1 # include < stdio .h >
2 # define pi 3.14
3
4 int main ()
5 {
6
7 printf ( " The value of pi : %.2 f " , pi ) ;
8 return 0;
9 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 17 / 50
2. The constants defined by #define and const
2.4. Difference between const and #define
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 18 / 50
3. Operators and expression
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 19 / 50
3. Operators and expression
3.1. Introduction
Operators in C are symbols that represent operations to be
performed on one or more operands.
They are the basic components of the C programming.
Operators helps us to perform some specific mathematical,
relational, bitwise, conditional, or logical computations on
values and variables.
The values and variables used with operators are called
operands.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 20 / 50
3. Operators and expression
3.2. Types of Operators in C
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Other Operators
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 21 / 50
3. Operators and expression
3.2.1. Arithmetic Operations in C
The arithmetic operators are used to perform
arithmetic/mathematical operations on operands.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 22 / 50
3. Operators and expression
3.2.1.1. Arithmetic Operations in C: examples
1 # include < stdio .h >
2
3 int main () {
4 int a = 25 , b = 5;
5
6 // using operators and printing results
7 printf ( " a + b = % d \ n " , a + b ) ;
8 printf ( " a - b = % d \ n " , a - b ) ;
9 printf ( " a * b = % d \ n " , a * b ) ;
10 printf ( " a / b = % d \ n " , a / b ) ;
11 printf ( " a % b = % d \ n " , a % b ) ;
12 printf ( " + a = % d \ n " , + a ) ;
13 printf ( " -a = % d \ n " , -a ) ;
14 printf ( " a ++ = % d \ n " , a ++) ;
15 printf ( "a - - = % d \ n " , a - -) ;
16
17 return 0;
18 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 23 / 50
3. Operators and expression
3.2.2. Relational Operators in C
Are used for the comparison of the two operands.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 24 / 50
3. Operators and expression
3.2.2.1. Relational Operators in C: examples
1 # include < stdio .h >
2 int main () {
3 int a = 25 , b = 5;
4
5 // using operators and printing results
6 printf ( " a = b : % d \ n " , a ) ;
7 printf ( " a += b : % d \ n " , a += b ) ;
8 printf ( " a *= b : % d \ n " , a *= b ) ;
9 printf ( " a ^= b : % d \ n " , a ^= b ) ;
10 printf ( " a >> b : % d \ n " , a >> b ) ;
11 printf ( " a %= b : % d \ n " , a %= b ) ;
12
13 return 0;
14 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 25 / 50
3. Operators and expression
3.2.3. Comparison Operators (1/2)
Comparison operators are used to compare two values
(variables ).
This is important in programming, because it helps us to find
answers and make decisions.
The return value of a comparison is either 1 or o, which
means true (1) or false (0).
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 26 / 50
3. Operators and expression
3.2.3. Comparison Operators (2/2)
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 27 / 50
3. Operators and expression
3.2.3.1. Comparison Operators: example
1 # include < stdio .h >
2 int main () {
3 int a = 25 , b = 5;
4
5 // using operators and printing results
6 printf ( " a < b : % d \ n " , a < b ) ;
7 printf ( " a > b : % d \ n " , a > b ) ;
8 printf ( " a <= b : % d \ n " , a <= b ) ;
9 printf ( " a >= b : % d \ n " , a >= b ) ;
10 printf ( " a == b : % d \ n " , a == b ) ;
11 printf ( " a != b : % d \ n " , a != b ) ;
12
13 return 0;
14 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 28 / 50
3. Operators and expression
3.2.4. Logical Operators (1/2)
Logical Operators are used to combine two or more
conditions/constraints or to complement the evaluation of the
original condition in consideration.
used to determine the logic between variables or values, by
combining multiple conditions.
The result of the operation of a logical operator is a
Boolean value either true or false.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 29 / 50
3. Operators and expression
3.2.4. Logical Operators (2/2)
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 30 / 50
3. Operators and expression
3.2.4.1. Logical Operators: example
1 # include < stdio .h >
2 int main () {
3 int a = 25 , b = 5;
4
5 // using operators and printing results
6 printf ( " a && b : % d \ n " , a && b ) ;
7 printf ( " a || b : % d \ n " , a || b ) ;
8 printf ( " ! a : % d \ n " , ! a ) ;
9
10 return 0;
11 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 31 / 50
3. Operators and expression
3.2.5. Bitwise Operators
The Bitwise operators are used to perform bit-level
operations on the operands.
The operators are first converted to bit-level and then the
calculation is performed on the operands.
Mathematical operations such as addition, subtraction,
multiplication, etc. can be performed at the bit level for
faster processing.
There are 6 bitwise operators in C:
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 32 / 50
3. Operators and expression
3.2.5.1. Bitwise Operators: example
1 # include < stdio .h >
2 int main () {
3 int a = 25 , b = 5;
4
5 // using operators and printing results
6 printf ( " a & b : % d \ n " , a & b ) ;
7 printf ( " a | b : % d \ n " , a | b ) ;
8 printf ( " a ^ b : % d \ n " , a ^ b ) ;
9 printf ( " ~ a : % d \ n " , ~ a ) ;
10 printf ( " a >> b : % d \ n " , a >> b ) ;
11 printf ( " a << b : % d \ n " , a << b ) ;
12
13 return 0;
14 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 33 / 50
3. Operators and expression
3.3. Booleans
Very often, in programming, you will need a data type that
can only have one of two values, like:
YES / NO
ON / OFF
TRUE / FALSE
For this, C has a bool data type, which is known as booleans.
Booleans represent values that are either true or false.
you must import the following header file to use it:
#include <stdbool.h>
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 34 / 50
3. Operators and expression
3.3.1. Booleans : examples
1 # include < stdio .h >
2 # include < stdbool .h >
3 int main () {
4 // Create boolean variables
5 bool isProgrammingFun = true ;
6 bool isFishTasty = false ;
7
8 // Return boolean values
9 printf ( " % d " , isProgrammingFun ) ; // Returns 1 ( true )
10 printf ( " % d " , isFishTasty ) ; // Returns 0 ( false )
11
12 printf ( " % d " , 10 == 10) ; // 1 , because 10 is equal to 10
13 printf ( " % d " , 10 == 8) ; // 0 , because 10 is not equal to 8
14 printf ( " % d " , 5 == 55) ; // 0 because 5 is not equal to 55
15 return 0;
16 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 35 / 50
4. Instructions and control
structures
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 36 / 50
4. Instructions and control structures
4.1. Conditions and If Statements
We have already learned that C supports the usual logical
conditions from mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
You can use these conditions to perform different actions for
different decisions.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 37 / 50
4. Instructions and control structures
4.1. Conditions and If Statements
C has the following conditional statements:
Use if to specify a block of code to be executed, if a
specified condition is true
Use else to specify a block of code to be executed, if
the same condition is false
Use else if to specify a new condition to test, if the
first condition is false
Use switch to specify many alternative blocks of code to
be executed
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 38 / 50
4. Instructions and control structures
4.2. The If Statements
Use the if statement to specify a block of code to be
executed if a condition is true.
Note that if is in lowercase letters. Uppercase letters (If
or IF) will generate an error.
1 if ( condition ) {
2 // block of code to be executed if the condition is true
3 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 39 / 50
4. Instructions and control structures
4.2.1. The If Statements: example
1 # include < stdio .h >
2 int main () {
3 int x = 20;
4 int y = 18;
5 if ( x > y ) {
6 printf ( " x is greater than y " ) ;
7 }
8 return 0;
9 }
We use two variables, x and y, to test whether x is greater
than y (using the > operator).
As x is 20, and y is 18, and we know that 20 is greater than
18, we print to the screen that "x is greater than y".
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 40 / 50
4. Instructions and control structures
4.3. The else Statement
Use the else statement to specify a block of code to be
executed if the condition is false.
1 if ( condition ) {
2 // block of code to be executed if the condition is true
3 } else {
4 // block of code to be executed if the condition is
false
5 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 41 / 50
4. Instructions and control structures
4.3.1. The else Statement: example
1 # include < stdio .h >
2 int main () {
3 int time = 20;
4 if ( time < 18) {
5 printf ( " Good day . " ) ;
6 } else {
7 printf ( " Good evening . " ) ;
8 }
9 }
Time (20) is greater than 18, so the condition is false.
Because of this, we move on to the else condition and print
to the screen Good evening.
If time was less than 18, the program would print Good day.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 42 / 50
4. Instructions and control structures
4.4. The else If Statement
Use the else if statement to specify a new condition if the
first condition is false.
1 if ( condition1 ) {
2 // block of code to be executed if condition1 is true
3 } else if ( condition2 ) {
4 // block of code to be executed if the condition1 is
false and condition2 is true
5 } else {
6 // block of code to be executed if the condition1 is
false and condition2 is false
7 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 43 / 50
4. Instructions and control structures
4.4.1. The else If Statement: example
1 # include < stdio .h >
2 int main () {
3 int time = 22;
4 if ( time < 10) {
5 printf ( " Good morning . " ) ;
6 } else if ( time < 20) {
7 printf ( " Good day . " ) ;
8 } else {
9 printf ( " Good evening . " ) ;
10 }
11 // Outputs " Good evening ."
12 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 44 / 50
4. Instructions and control structures
4.4.1. The else If Statement: example explained
In the example above, time (22) is greater than 10, so the
first condition is false.
The next condition, in the else if statement, is also false,
so we move on to the else condition since condition1 and
condition2 is both false - and print to the screen "Good
evening".
However, if the time was 14, our program would print "Good
day."
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 45 / 50
4. Instructions and control structures
4.5. The switch Statement
Instead of writing many if...else statements, you can use the
switch statement.
The switch statement selects one of many code blocks to be
executed:
1 switch ( expression ) {
2 case x :
3 // code block
4 break ;
5 case y :
6 // code block
7 break ;
8 default :
9 // code block
10 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 46 / 50
4. Instructions and control structures
4.5.1. The switch Statement: functionment
The switch expression is evaluated once
The value of the expression is compared with the values of
each case
If there is a match, the associated block of code is executed
The break statement breaks out of the switch block and stops
the execution
The default statement is optional, and specifies some code to
run if there is no case match
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 47 / 50
4. Instructions and control structures
4.5.2. The switch Statement : example
1 # include < stdio .h >
2 int main () {
3 int day = 4;
4 switch ( day ) {
5 case 1:
6 printf ( " Monday " ) ;
7 break ;
8 case 2:
9 printf ( " Tuesday " ) ;
10 break ;
11 case 3:
12 printf ( " Wednesday " ) ;
13 break ;
14 case 4:
15 printf ( " Thursday " ) ;
16 break ;
17 case 5:
18 printf ( " Friday " ) ;
19 break ;
20 case 6:
21 printf ( " Saturday " ) ;
22 break ;
23 case 7:
24 printf ( " Sunday " ) ;
25 break ;
26 }
27 }
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 48 / 50
4. Instructions and control structures
4.5.3. The switch Statement: break
When C reaches a break keyword, it breaks out of the switch
block.
This will stop the execution of more code and case testing
inside the block.
When a match is found, and the job is done, it’s time for a
break. There is no need for more testing.
A break can save a lot of execution time because it ignores
the execution of all the rest of the code in the switch
block.
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 49 / 50
==END==
(B.i.t/E.E, S3) Engineering Practical Programming 11 octobre 2024 50 / 50