Académique Documents
Professionnel Documents
Culture Documents
Cours 1
Environnement : Linux
▶ éditeurs : gedit/emacs/vi/gvim
▶ compilateur : gcc
▶ débogueur : gdb, ddd
Le Langage C : historique
▶ impératif
▶ bas-niveau
▶ typé
▶ compilé
Comparaison de différents langages
Pereira, R., Couto, M., Ribeiro, F., Rua, R., Cunha, J., Fernandes, J. P., & Saraiva, J. (2017). Energy efficiency
across programming languages : how do energy, time, and memory relate ?. In Proceedings of the 10th ACM
SIGPLAN International Conference on Software Language Engineering (pp. 256-267).
Premier programme
Premier programme
1 i n t main ( ) {
2 p r i n t f ( ” H e l l o World \n” ) ;
3 return 0;
4 }
Les fonctions
1 void H e l l o ( ) {
2 p r i n t f ( ” H e l l o World \n” ) ;
3 }
4
5 i n t main ( ) {
6 H e l l o ( ) ; // A p p e l de l a f o n c t i o n
7 return 0;
8 }
1 i n t main ( ) {
2 Hello () ;
3 return 0;
4 }
5
6 void H e l l o ( ) {
7 p r i n t f ( ” H e l l o World \n” ) ;
8 }
1 i n t main ( ) {
2 Hello () ;
3 return 0;
4 }
5
6 void H e l l o ( ) {
7 p r i n t f ( ” H e l l o World \n” ) ;
8 }
1 v o i d H e l l o ( i n t an n ee ) {
2 p r i n t f ( ” H e l l o World %d\n” , a nn ee ) ;
3 }
4
5 i n t main ( ) {
6 H e l l o ( 2 0 2 3 ) ; // A p p e l de l a f o n c t i o n
7 return 0;
8 }
Les fonctions : prototypes
5 i n t main ( ) {
6 H e l l o ( 2 0 2 3 ) ; // A p p e l de l a f o n c t i o n
7 return 0;
8 }
La fonction printf
Exemples :
1 p r i n t f ( ” un e n t i e r %d , un f l o a t %f \n” , 2 0 2 3 , 3 . 1 4 ) ;
2 p r i n t f ( ” un c a r a c t è r e %c \n” , ’ b ’ ) ;
Arguments :
▶ une chaı̂ne de caractères entre ” ” (le format) contenant
éventuellement %f pour les float , %d pour les int et %c pour
les char
▶ les valeurs ou variables à afficher
Variables : déclaration et affectation
En C, il faut obligatoirement déclarer les variables :
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t d l i b . h>
3 #i n c l u d e <math . h>
4
5 float aire triangle ( float a , float b , float c) {
6 float p;
7 p = (a + b + c ) / 2;
8 return sqrt (p ∗ (p − a) ∗ (p − b) ∗ (p − c ) ) ;
9 }
10
11 i n t main ( ) {
12 f l o a t a =0 , b=1 , c =3 , d = 3 . 5 ;
13 a= a i r e t r i a n g l e ( b , c , d ) ;
14 p r i n t f ( ”L ’ a i r e du t r i a n g l e e s t %f \n” , a ) ;
15 return 0;
16 }
référence : () [] -> .
unaire : ! ++ + - * & (type) sizeof
arithmétique : * / %
arithmétique : + -
relationnels : < <= > >=
relationnels : == !=
logique : &&
logique : ||
affectation : =
1 d e f p e r i m e t r e ( l a r g e u r : i n t , l o n g u e u r : i n t ) −> i n t :
2 ””” h y p o t h e s e : l o n g u e u r >= l a r g e u r >= 0
3 r e t o u r n e l e p e r i m e t r e du r e c t a n g l e d e f i n i p a r
4 s a l a r g e u r e t s a l o n g u e u r . ”””
5 return 2 ∗ ( l a r g e u r + longueur )
1 /∗ h y p o t h e s e : l o n g u e u r >= l a r g e u r >= 0
2 r e t o u r n e l e p e r i m e t r e du r e c t a n g l e d e f i n i p a r
3 s a l a r g e u r e t s a l o n g u e u r . ∗/
4 int perimetre ( int largeur , int longueur ) {
5 return 2 ∗ ( l a r g e u r + longueur ) ;
6 }
De Python à C : spécification
Méthode : exemple pour la fonction perimetre :
1. Que doit calculer la fonction ?
▶ périmètre du rectangle défini par sa largeur et sa longueur.
2. Quels sont les paramètres (nombre, nom, type) ?
▶ les deux côtés largeur et longueur,
▶ tous les deux de type int.
3. Quelles sont les hypothèses sur les paramètres ?
▶ longueur >= largeur >= 0
4. Quel est le type de la valeur de retour ?
▶ le périmètre du rectangle est de type int.
1 /∗ h y p o t h e s e : l o n g u e u r >= l a r g e u r >= 0
2 r e t o u r n e l e p e r i m e t r e du r e c t a n g l e d e f i n i p a r
3 s a l a r g e u r e t s a l o n g u e u r . ∗/
4 int perimetre ( int largeur , int longueur ) {
5 return 2 ∗ ( l a r g e u r + longueur ) ;
6 }
De Python à C : ce qui change (1)
C : typage explicite
1 int perimetre ( int largeur , int longueur )
1 /∗ h y p o t h e s e : l o n g u e u r >= l a r g e u r >= 0
2 r e t o u r n e l e p e r i m e t r e du r e c t a n g l e d e f i n i p a r
3 s a l a r g e u r e t s a l o n g u e u r . ∗/
De Python à C : ce qui change (3)
Bloc :
▶ Python : indentation, une instruction par ligne
▶ C : accolades, point virgule en fin d’instruction
1 d e f p e r i m e t r e ( l a r g e u r : i n t , l o n g u e u r : i n t ) −> i n t :
2 ””” h y p o t h e s e : l o n g u e u r >= l a r g e u r >= 0
3 r e t o u r n e l e p e r i m e t r e du r e c t a n g l e d e f i n i p a r
4 s a l a r g e u r e t s a l o n g u e u r . ”””
5 return 2 ∗ ( l a r g e u r + longueur )
1 /∗ h y p o t h e s e : l o n g u e u r >= l a r g e u r >= 0
2 r e t o u r n e l e p e r i m e t r e du r e c t a n g l e d e f i n i p a r
3 s a l a r g e u r e t s a l o n g u e u r . ∗/
4 int perimetre ( int largeur , int longueur ) {
5 return 2 ∗ ( l a r g e u r + longueur ) ;
6 }
Les structures de contrôle
▶ if /else
▶ while et do ... while
▶ for
Alternative ( if et else )
if ( expression ) {
instructions ;
}
else {
instructions ;
}
Python
C
1 def v a l e u r a b s o l u e ( x : f l o a t )
−> f l o a t : 1 float valeur absolue ( float x)
2 ””” r e t o u r n e l a v a l e u r {
a b s o l u e de x . 2 f l o a t abs x ;
3 ””” 3 i f ( x >= 0 ) {
4 # a b s x : Number 4 a b s x= x ;
5 abs x = 0 5 } else {
6 if x >= 0 : 6 a b s x=−x ;
7 abs x = x 7 }
8 else : 8 return abs x ;
9 a b s x = −x 9 }
10 return abs x
if ( expression ) {
if ( expression ) {
instructions ;
} else {
instructions ;
}
}
else {
if ( expression ) {
instructions ;
} else {
instructions ;
}
}
Alternatives imbriquées
1 /∗ h y p o t h è s e : n >= 1 [ . . ]
1 def somme entiers (n : i n t )
∗/
−> i n t :
2 int somme entiers ( int n){
2 # i : int
3 int i = 1;
3 i = 1
4 int s = 0;
4 # s : int
5 w h i l e ( i <= n ) {
5 s = 0
6 s = s + i;
6 w h i l e i <= n :
7 i = i + 1;
7 s = s + i
8 }
8 i = i + 1
9 return s ;
9 return s
10 }
1 i n t main ( ) {
2 i n t i = 1 , n =1;
3 do {
4 p r i n t f ( ” i :%d\n” , i ) ;
5 i = i + 1;
6 } while ( i < n ) ;
7 return 0;
8 }
1 int i ;
2 f o r ( i = 1 ; i <= n ; i ++) {
3 s = s + i;
4 }
1 int i ;
2 f o r ( i = 1 ; i <= n ; i ++) {
3 s = s + i;
4 }
for
1 for ( expression1 ; expression2 ; expression3 ) {
2 instructions ;
3 }
equivaut à :
while
1 expression1 ;
2 while ( expression2 ) {
3 instructions ;
4 expression3 ;
5 }
C’est tout pour aujourd’hui !