Annex Basic Elements of c Language
Annex Basic Elements of c Language
Faculty of Sciences
Department of Computer Science
Computer Science Engineering
Semester 1 (1st year)
2024-2025
Available at https://sites.google.com/a/esi.dz/informatiqueblida/algorithmique-et-structure-
de-donn%C3%A9es
CONTENT
Presentation
C Programming
C Language Formalism
2
PRESENTATION
Among all existing programming languages, the C language is certainly the most
famous. It has often, in the past, been referred to as a universal language in the
sense that almost any computer system can be developed from this language. For
example, most current operating systems are programmed in C (at least their
kernel).
It is a low level language (close to the machine) in the sense that it allows access to
data (bits, bytes, addresses) that machines manipulate and which are not often
available from high level languages such as Java, C#, Python, etc.
Compared to other languages (notably Java), the C language makes it possible to
develop more compact and much faster applications, but in much more time.
It is very important to master the C language in order to better understand the
languages of the highest level.
3
WHY LEARN?
Its popularity: It has a very large community and many tutorials and
documentation. In addition, there are many programs and libraries
developed in C.
Its speed: making it a language of choice for any program where response
time (execution) is crucial.
Its lightness: which makes it useful for embedded programs where the
available memory is low.
At first (PW1 and PW2), we will use these tools separately in order to
understand the process of compilation and execution via the command
prompt.
But later (PW3 and PW4), we will use a development environment (IDE,
Integrated development environment) that integrates these two tools and
others (debugger, etc.) as Code::Blocks, Visual C++, etc.
5
PROGRAMMING
Linker
(gcc –o)
Execution 6
Results (.\prog) prog.exe
Executable
STANDARDS
In 1973, the C language was officially announced by Dennis M. Ritchie based on the
BCPL and B languages.
In 1989, the ANSI (American National Standards Institute) committee standardized
the language by creating the C-ANSI standard (also commonly referred to as C89).
In 1990, the C language was standardized by ISO (International Standardization
Organization), then referred to as C90.
In 1999, the ISO committee published a second version of its standard: we then speak
of C99 which incorporates many points (Boolean type, comment on a single line, no
more obligation to declare the variables at the head of the block, etc.). In this
course, we will use this standard.
Other new versions of the standard have been proposed by ISO: C11 in 2011 and C18
7
in 2018 which add some new features (threads, bug fix, etc.).
C LANGUAGE FORMALISM
C language identifiers
They make it possible to give a name to an entity of the program (variable,
function, or type)
An identifier begins with an alphabetic letter, whether or not followed by
alphabetic letters, numbers, or "_".
Some rules for choosing identifiers:
They must not be chosen from the keywords: char, double, float, int,
long, short, signed, unsigned, enum, const, break, case, default, for,
while, do, if, else, switch, return, etc.
They must respect the case (upper or lower case).
The compiler may truncate identifiers to a certain length (usually longer
8
than 31 characters).
C LANGUAGE FORMALISM
#include <library>
Structure of a program
int main ()
ALGORITHM <identifier> {
<declarations> <declarations>;
BEGIN < Body: instructions >;
< Body: instructions> return 0;
END }
The declaration part can be in the main function, as it can be before the main
function.
The main function is the entry point of the program: the first one that will be
executed.
Each BEGIN corresponds to an opening brace {
Each END corresponds to a closing brace }
9
Each statement ends with a semicolon ;
Each primitive action (assignment, read and write) ends with a semicolon ;
C LANGUAGE FORMALISM
Declaration of variables
Declaration of constants
Example:
CONST PI = 3.14
CONST float PI = 3.14
VAR
int X;
X: INTEGER
float Y;
Y: REAL 10
bool B;
B: BOOLEAN
char C;
C: CARACTER
C LANGUAGE FORMALISM
Standard Type: Integer
Data type Algorithms Meaning N Range of values
Oct
short [int] integer Short integer 2 -32 768 à 32 767
unsigned short [int] Unsigned integer 2 0 à 65 535
int Integer 2 -32 768 à 32 767
4 -2 147 483 648 à 2 147 483
647
unsigned [int] [int] Unsigned integer 2 0 à 65 535
OPTIONAL 4 0 à 4 294 967 295
long [int] Long integer 4 -2 147 483 648 à 2 147 483
647
unsigned long [int] Unsigned Long 4 0 à 4 294 967 295
long long [int] Double long integer 8 -9 223 372 036 854 775 807
à 9 223 372 036 854 775
807 11
unsigned long long [int] Signed double long 8 0 à 18 446 744 073 709 551
integer 615
C LANGUAGE FORMALISM
Standard types: real and character
Data type Algorithms Meaning N Range of values
Oct
float real Floating or real 4 3,4 x 10−38 à 3,4 x 1038 env.
double Double Float 8 1,7 x10 −308 à 1,7 x10 308
env.
long double Float Double Long 10 3, 4 x10−4932 à 3, 4 x104932
env.
char char Character 1 - 128 à 127
unsigned char Unsigned character 1 0 à 255
12
C LANGUAGE FORMALISM
Standard Type: Boolean
There are two ways to declare a variable “b” of the Boolean type in the C99
standard:
Declare this variable as an integer (….. int) then test it to find out its
logical value: any integer value other than 0 will be considered true and the
value 0 will be considered false.
Use the header <stdbool.h> that defines three
macros, bool, true and false to improve program readability.
Declaration: bool b;
Logical test: (b == true) or (b == false)
13
C LANGUAGE FORMALISM
Non-standard types
14
C LANGUAGE FORMALISM
Assignment
Relational <, <=, >, >=, =, ≠, ==, !=, <, <=, >, >= Equality ≡ double15
=
C LANGUAGE FORMALISM
Assignment Operators
Note that in the C language, the assignment is an operator (not an
instruction) evaluated from right to left. In addition, there is a set of other
operators that are combinations between an arithmetic operator, and the
assignment operator.
int t = ++v; v = 6; t = 6;
C LANGUAGE FORMALISM
Operator priority: This table shows the operator precedence table: this is a
ranking of the operators of the C language from the highest priority (at the
top of the table) to the lowest priority (at the bottom).
Symbol Operators Sense of evaluation
Bracket (), ++, - - (post) Left (L) Right (R)
unary operators -, !, ++, - - (pre) R L
Arithmetic operator */% L R
Arithmetic operator +- L R
Comparison Operators < <= >= > L R
Comparison Operators == != L R
Logical AND && L R
Logical OR || L R 18
Conditional operator ?: R L
Assignment Operators = += -= *= /= %= R L
C LANGUAGE FORMALISM
Alternative Actions
It is the only operator of the C language that works on three operands: the
condition, the value to be calculated if the condition is true and the value to
be calculated if the condition is false.
As a result, this operator is broken down into two parts: the character "?
"separates the condition from the expression to be evaluated if the condition
is true and the character": "to separate the expression associated with the
true condition and that associated with the false condition.
20
C LANGUAGE FORMALISM
Alternative Actions: At the first constant whose value is equal to that of expression, the
corresponding statement(s) (of one or more blocks) are executed until the first break
statement is encountered; indeed, the encounter of a break statement terminates the
execution of the switch statement.
stdout, the standard output that sends the data that the program generates to the
screen.
stderr, the standard output of the errors that will be displayed on the screen.
This library must be included in the C program (before the main function) in
order to use the reading (scanf) and writing (printf) functions.
24
C LANGUAGE FORMALISM
Reading
The "scanf" function makes it possible to read values on the standard input
unit (stdin), according to a format specified as an argument and write them
into memory cases whose addresses are provided as arguments.
scan ([f],var1, var2, … , varn) scanf (format, & var1, & var2,..., & var25n)
C LANGUAGE FORMALISM
Reading scanf (format, & var1, & var2,..., & varn )
The "format" string contains the format specifications of the variables (var1,
var2,..., varn) to be retrieved from the input stream.
Introduced by the % sign, these specifications are:
code Type of variable/expression to read/write
% Read a percentage % (use %%)
d ou o ou x signed integer expressed in octal, decimal, or hexadecimal base
h, l signed short or long integer expressed in decimal base
u Unsigned integer
number with decimal part in decimal point notation, in exponential
e ou f ou g
notation or the shortest (writing)
c character
If the value of a variable is not of the type indicated by the format, it will 26be
converted.
C LANGUAGE FORMALISM
Strings of type %zz will be replaced on printing by the values of expressions exp1,
exp2, ... expn.
29
If the value of an expression is not of the type indicated by the format, it will be
converted.
C LANGUAGE FORMALISM
Standard mathematical functions
The <stdlib.h> and <math.h> libraries provide the main mathematical functions:
ALGORITHM abs_parite
VAR x : INTEGER
BEGIN
PRINT (" give en integer x = ")
SCAN (X)
IF (x >= 0) THEN PRINT (" The absolute value of is : ", x )
ELSE PRINT (" The absolute value of is : ", (-1) * x)
IF (x MOD 2 = 0) THEN PRINT ("x est even ")
ELSE PRINT ("x est odd ")
END 32
C LANGUAGE FORMALISM
Example: translate the following algorithm into C program
33
C LANGUAGE FORMALISM
Example: translate the following algorithm into C program
34
COURSE SOURCES
S. AROUSSI (2017), Cours Algorithmique & Structures de Données (ASD), 2nd year licence,
Computer Science Department, Saad Dahlab University, Blida.
35