0% found this document useful (0 votes)
7 views

Annex Basic Elements of c Language

The document provides an overview of the C programming language, highlighting its significance as a low-level language that offers speed, portability, and a large community. It covers essential elements such as programming standards, language formalism, variable declaration, control structures, and input/output functions. The content is aimed at first-year computer science students at Saad Dahlab University, focusing on foundational knowledge necessary for mastering C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Annex Basic Elements of c Language

The document provides an overview of the C programming language, highlighting its significance as a low-level language that offers speed, portability, and a large community. It covers essential elements such as programming standards, language formalism, variable declaration, control structures, and input/output functions. The content is aimed at first-year computer science students at Saad Dahlab University, focusing on foundational knowledge necessary for mastering C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Saad Dahlab University - Blida1

Faculty of Sciences
Department of Computer Science
Computer Science Engineering
Semester 1 (1st year)

ANNEX: BASIC ELEMENTS OF C


LANGUAGE

Mrs AROUSSI ([email protected])

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.

Its portability: a program developed in C theoretically works on any


platform (Windows, Linus, Unix, Mac, etc.) 4
REQUIRED TOOLS
Programmers need two tools to program in C:
a text editor and compiler for the C language.

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

Algorithm Text Editor Compilation


prog.c
(gcc –c)
Source code

prog.o object file


Library

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

VAR <identifier> : TYPE TYPE <identifier> ;

Declaration of constants

CONST <identifier> = value CONST TYPE <identifier> = value;

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

Type algorithmic language C language


Enumerated TYPE <Id> = ( a , a , …, a ) enum <Id>{a1, a2, …, an};
1 2 n

Interval TYPE <Id> = C1 .. Cn It does not exist, just use an


integer (int) and a condition that
verifies that we are in the
interval

14
C LANGUAGE FORMALISM
Assignment

<Id> <valeur> ou <Id> <identificateur> ou <Id> <expression>

<Id> = <valeur> ou <Id> = <identificateur> ou <Id> = <expression>


Operators

Language/Operators Algorithmic C language Comments

Arithmetic +, -, *, /, mod, div +, -, *, /, % div ≡ integer /integer

Logic NOT, AND, OR, !, &&, ||,

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.

Operator Example equivalent to


+= a += 10 a = a + 10
-= a -= 10; a = a - 10;
*= a *= 2; a = a * 2;
/= a /= 2; a = a / 2;
16
%= a %= 2; a = a % 2;
C LANGUAGE FORMALISM
The increment operator (++) and decrement operator (--) are unary
operators that can act on an integer (including character) or real variable. The
variable is in fact increased by 1 (incrementation) or decreased
(decrementation) by 1, while the value returned is either the old value of the
variable (post-incrementation or post-decrementation) or the new one (pre-
incrementation and pre-decrementation).
Example: Give result of variables to each statement
Results
int i = 4; i = 4;
int u = i++; u = 4; i = 5;
int v = i--; v = 5; i = 4;
int w = --u; u = 3; w = 3; 17

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

Algorithmic Language C Language


if (condition) then if (condition)
BEGIN {
blocif blocif;
ENDif }
if (condition) then if (condition)
BEGIN {
blocif blocif;
ENDif }
else else
BEGIN { 19
blocelse blocelse;
ENDelse }
C LANGUAGE FORMALISM
The conditional operator is used to evaluate a value according to a
condition.
condition ? ifTrueExpression : ifFalseExpression ;

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.

Algorithmic Language C Language


switch (expression) switch (expression)
BEGIN {
case c1 : begin bloc1 end case c1 : bloc1 ; [break;]
case c2 : begin bloc2 end case c2 : bloc2 ; [break;]
. .
. .
case cn: begin blocn end case cn: blocn ; [break;]
21
[default: begin blocdefault end] [default: blocdefault ;]
ENDswitch }
C LANGUAGE FORMALISM
Iterative actions
Algorithmic Language C Language
for ct iv to fv [step p] do for(ct=iv ;ct<=fv ;ct=ct+p)
begin {
bloc bloc;
endfor
}
while (condition) do while (condition)
begin {
bloc bloc;
endwhile }
do do
begin {
bloc bloc; 22
end }
while (condition) while (condition)
C LANGUAGE FORMALISM
Iterative actions: FOR
The for loop is broken down into three separately optional parts (or three clauses).
Initialization allows you to define loop start conditions,
Condition is a Boolean expression that must be true for the continuation of the loop, i.e. a
new execution of < instruction block >,
Iteration performs a state change of the variable (s) used in the loop iteration control.
The Initialization and Iteration clauses can be composed of multiple statements separated
by a comma.

for (<Initialization>; <Condition>; <Iteration>)


{
<instruction block>; for (int j=0, int i = j+5 ; (j<10)||(i<100); j++, i* = 2) 23
}
printf("j = %d , i = %d ", j, i) ;
C LANGUAGE FORMALISM
Reading - Writing Functions
The header <stdio.h> (short for standard in-out) provides three streams of
data that can be used directly:
stdin, the standard input that sends the data from the keyboard to the program.

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.

scanf (format, arg1,arg2,...,argn);


Often, the arguments argi have memory addresses where variables are
stored and are therefore of the form &vari where vari is the identifier of a
variable and & is the addressing operator:

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

Declaration reading writing external format


int i; scanf("%d",&i); printf("%d",i); decimal Integers can be expressed in

int i; scanf("%o",&i); printf("%o",i); octal three different numerical

int i; scanf("%x",&i); printf("%x",i); hexadecimal bases, by:


unsigned int i; scanf("%u",&i); printf("%u",i); decimal decimal (the most
short j; scanf("%hd",&j); printf("%d",j); decimal classic use)
short j; scanf("%ho",&j); printf("%o",j); octal octal (the number is
short j; scanf("%hx",&j); printf("%x",j); hexadecimal simply prefixed with a
unsigned short j; scanf("%hu",&j); printf("%u",j); decimal character 0)
long k; scanf("%ld",&k); printf("%d",k); decimal
hexadecimal (prefixed
long k; scanf("%lo",&k); printf("%o",k); octal
with both 0x 27
long k; scanf("%lx",&k); printf("%x",k); hexadecimal
characters).
unsigned long k; scanf("%lu",&k); printf("%u",k); decimal
C LANGUAGE FORMALISM
Example
The reals are represented in
Declaration reading writing external format
float l; scanf("%f",&l); printf("%f",l); Decimal point
floating point which can be

float l; scanf("%e",&l); printf("%e",l); Exponential expressed in notation:

float l; printf("%g",l); SHORTEST decimal (the most


double m; scanf("%lf",&m); printf("%f",m); Decimal point conventional use), e,g:
double m; scanf("%le"&m); printf("%e",m); Exponential double nd = 3.14159254;
double m; printf("%g",m); SHORTEST Exponential
long double n; scanf("%lf"&n); printf("%Lf",n); Decimal point (sign 0,mantissa Bexpon
long double n; scanf("%le"&n); printf("%Le",n); Exponential ent''.), e.g double ne =
long double n; printf("%Lg",n); SHORTEST
3.2e-10; 28
char o; scanf("%c",&o); printf("%c",o); character
C LANGUAGE FORMALISM
Writing
The "fprintf" function allows values to be written to the standard output unit
(stdout or screen), according to the "format" specified in the argument.
print ([f],exp1, exp2, … , expn) printf (format, exp1, exp2, … , expn)
As well as reading, the "format" parameter contains the characters to be displayed
and the writing format specifications corresponding to the parameters exp1, exp2 , ...,
expn .

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:

Library Function Description


<stdlib.h> abs Absolute value of integers (int)
labs Absolute value of long integers
div Obtaining quotient and remainder
<math.h> fabs Absolute value of floats
pow Power
sqrt Square root
exp Exponential
log Logarithm
cos, acos cosine, arc cosine 30
sin, asin sine, arc sine
tan, atan tangent, arc tangent
C LANGUAGE FORMALISM
Comments:
To comment on code written in program C, you can use:
The /* and */ symbols surrounding the comment. The latter can extend
over several lines, e.g /* This is a comment on several possible lines */
The // symbol (two slashes) preceding the comment, limited to the rest of
the line, e,g: // This is a one-line comment
Comments can be placed anywhere in the program without cutting a word of
the program in half.
They are ignored when running the program.
Their use, although optional, is fundamental. Indeed, they allow an easier
31
understanding of a code left behind for weeks and are useful when
maintaining programs
C LANGUAGE FORMALISM
Example: translate the following algorithm into C program

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.

Ressources Pédagogiques des programmeurs, Available at https://koor.fr/C/Index.wp.


Accessed October 2023.

35

You might also like