++ Programmation en langage Les fondamentaux Alain hillès 祁冲 ParisTech Shanghai Jiao Tong 上海交大 巴黎高科卓越工程师学院 3 septembre 015 015 年 9 月 3 日 乙未年八月十一 1
Mots réservés ++ auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
Mots réservés Qu est-ce qu un mot réservé? ++ On ne peut pas l utiliser comme nom de variable ou de fonction La compilation génère un erreur de syntaxe! 5 i n t x =1; 6 p r i n t f ( " x=%d ", x ) ; 7 } 5 i n t i f =1; 6 p r i n t f ( " i f =%d ", i f ) ; 7 } 0 warnings errors, 0 warnings 3
Les types ++ Nom du type Syntaxe Mots Valeurs aractère char x 1 [[ 17, 18]] aractère non signé unsigned char x 1 [[ [[0, 55]] Entier int x 15, 15 1 ]] [[ Entier non signé unsigned int x 0, 16 1 ]] [[ Entier long long int x 4 31, 31 1 ]] [[ Entier long non signé unsigned long int x 4 0, 3 1 ]] [ Flottant float x 4 [ 1.17 10 38, 3.40 10 38] Double double x 8. 10 308, 1.79 10 308] Double long long double x 10 4
Les types ++ 5 p r i n t f ( " Les c a r a c t e r e s : %zu, %zu \ n ", 6 s i z e o f ( char ), s i z e o f ( unsigned char ) ) ; 7 p r i n t f ( " Les e n t i e r s : %zu, %zu, %zu \ n ", 8 s i z e o f ( i n t ), s i z e o f ( long i n t ), s i z e o f ( long long i n t ) ) 9 p r i n t f ( " Les f l o t t a n t s : %zu, %zu, %zu \ n ", 10 s i z e o f ( f l o a t ), s i z e o f ( double ), s i z e o f ( long double ) ) ; 11 } Les caracteres : 1, 1 Les entiers : 4, 4, 8 Les flottants : 4, 8, 1 5
Les types #include < l i m i t s. h> 3 #include < f l o a t. h> 4 5 void main ( ) 6 { ++ 7 p r i n t f ( "Minimum : %d, maximum : %d \ n ", INT_MIN, INT_MAX ) ; 8 p r i n t f ( "Minimum : %f, maximum : %f \ n ",FLT_MIN,FLT_MAX ) ; 9 } Minimum : -147483648, maximum : 147483647 Minimum : 0.000000, maximum : 34083466385885981170418348451695440.000000 On reconnaît 31 1 et... Les informations peuvent se trouver sur https://en.wikipedia.org/wiki/_data_types 6
Lire et écrire x vaut 16 ++ Les fonctions scanf (lire) et printf (écrire). Nécessite un format correspondant au type On lit à une adresse (&), on écrit une valeur. 5 i n t x ; 6 p r i n t f ( " Donner une v a l e u r pour x : " ) ; 7 s c a n f ( "%d ",&x ) ; 8 p r i n t f ( " x v a u t %d \ n ", x ) ; 9 p r i n t f ( " Donner une v a l e u r en b ase 8 pour x : " ) ; 10 s c a n f ( "%o ",&x ) ; 11 p r i n t f ( " x v a u t en base 8 : %o e t en b ase 10 : %d \ n ", x, x ) ; 1 } Donner une valeur pour x : 16 Donner une valeur en base 8 pour x : 546 x vaut en base 8 : 546 et en base 10 : 358 7
Lire et écrire ++ Type Format Déclaration char %c char x= a ; int %d ou %i int x=1; (base 8) %o int x=03 (base 16) %x int x=0x13 unsigned int %u unsigned int x=; float %f float x=1.5 double %lf double x=1.5 Les informations peuvent se trouver sur https://en.wikipedia.org/wiki/_data_types 8
Lire et écrire ++ Les caractères d échappement (caractères spéciaux) : aractère Échappement \n Nouvelle ligne \t Tabulation \b Retour arrière \" " \\ \ 9
onversion de types 4 { ++ On peut convertir un objet d un type donné en un autre type en utilisant : onversion automatique (dangereux!) : 5 char x = a ; 6 i n t i = x ; 7 p r i n t f ( " Le c a r a c t e r e %c a pour code %i \ n ", x, i ) ; 8 } Le caractere a a pour code 97 10
onversion de types ++ onversion signalée (cast) : 5 char x = a ; 6 i n t i = ( i n t ) x ; 7 p r i n t f ( " Le c a r a c t e r e %c a pour code %i \ n ", x, i ) ; 8 } Le caractere a a pour code 97 11
onversion de types Fonction de conversion : 3 i n t a t o i ( char s [ ] ) 9 } ++ 5 i n t i, n =0; 6 f o r ( i =0; s [ i ] >= 0 && s [ i ] <= 9 ; i ++) 7 n = 10 n +( s [ i ] 0 ) ; 8 return n ; 10 void main ( ) 11 { 1 char s [ ] = " 13 " ; 13 p r i n t f ( "On o b t i e n t : %i \ n ", a t o i ( s ) ) ; 14 char t [ ] = " abc " ; 15 p r i n t f ( "On o b t i e n t m a i n t e n a n t : %i \ n ", a t o i ( t ) ) ; 16 } On obtient : 13 On obtient maintenant : 0 1
onversion de types ++ Utilisation des fonctions de bibliothèque : ctype.h tolower toupper toascii. 13
Opérateurs ++ Arithmétiques : +,,, / Relations : <, >, <=, >=, ==,! = Logiques : and (ou &&), or (ou ) Incrémentations : ++, Affectations : =, + =, =, =, / = Attention! Ne pas confondre = (affectation) et == (test d égalité)! 14
Opérateurs ++ 5 i n t x =5; 6 i f ( x ==6) 7 p r i n t f ( " x v a u t 6 \ n " ) ; 8 e l s e 9 p r i n t f ( " x ne v a u t \ 10 pas 6 \ n " ) ; 11 } 5 i n t x =5; 6 i f ( x =6) 7 p r i n t f ( " x v a u t 6 \ n " ) ; 8 e l s e 9 p r i n t f ( " x ne v a u t \ 10 pas 6 \ n " ) ; 11 } x ne vaut pas 6 x vaut 6 15
++ Incrémentation/décrémentation 5 i n t x =3, y ; 6 y=++x ; 7 p r i n t f ( " x= %d, y= %d \ n ", x, y ) ; 8 y=x ++; 9 p r i n t f ( " x= %d, y= %d \ n ", x, y ) ; 10 } x= 4, y= 4 x= 5, y= 4 16
Opérations bit à bit ++ Les entiers (types char, int, short, long, etc.) peuvent être traités en base. Logiques : & (et), (ou) et ˆ (ou exclusif) Décalages (division ou multiplication par ) : >> et << omplément à 1 : 5 i n t x =1, y =1; 6 p r i n t f ( " x e t y= %d \ n ", x&y ) ; 7 p r i n t f ( " x ou y= %d \ n ", x y ) ; 8 p r i n t f ( " x ou e x c l u s i f y= %d \ n ", x^y ) ; 9 p r i n t f ( " y s u r = %d \ n ", y=y > >1); 10 p r i n t f ( " y / f o i s = %d \ n ", y < <1); 11 p r i n t f ( " complement a 1 de x= %d \ n ",~ x ) ; 1 } 17
Opérations bit à bit ++ Un exemple : compter les bits égaux à 1. 5 unsigned i n t x, x0 ; 6 i n t b ; 7 p r i n t f ( " E n t r e z x = " ) ; s c a n f ( "%u ",&x ) ; x0=x ; 8 f o r ( b =0; x! = 0 ; x=x > >1) 9 i f ( x&1) b ++; 10 p r i n t f ( " I l y a %d b i t s a 1 dans %u \ n ", b, x0 ) ; 11 } Entrez x = 14 Il y a 4 bits a 1 dans 14 18
Affectations ++ On a les équivalences : { y = x; y = x++; x = x + 1; et y = ++x; { y+ = x; y = y + x; y = x; y = y x; x = x + 1; y = x; y = x; y = y x; et y/ = x; y = y/x; Et de même, avec les autres opérateurs... 19
Expressions conditionnelles ++ Expression condensée : <cond>? <sivrai> : <sifaux> ; 5 i n t x =5; 6 p r i n t f ( " x e s t i l e g a l a 5? %s \ n ", x==5? " Oui " : "Non" ) ; 7 x ++; 8 p r i n t f ( " x e s t i l e g a l a 5? %s \ n ", x==5? " Oui " : "Non" ) ; 9 } x est-il egal a 5? Oui x est-il egal a 5? Non 0
Expressions conditionnelles ++ Expression détaillée : if(<cond>) <sivrai>; else <sifaux>; 5 i n t x =5; 6 p r i n t f ( " x e s t i l e g a l a 5? " ) ; 7 i f ( x ==5) 8 p r i n t f ( "%s \ n ", " Oui " ) ; 9 e l s e 10 p r i n t f ( "%s \ n ", "Non" ) ; 11 } x est-il egal a 5? Oui 1
Priorités ++ Les opérateurs ont des priorités les uns par rapport aux autres! Éviter les priorités implicites! Mettre des parenthèses. 5 i n t a, b, c, d ; 6 a =1; b =; c =3; d =4; 7 p r i n t f ( "%d \ n ", a+b c / d ) ; 8 p r i n t f ( "%d \ n ", a +( b c ) / d ) ; 9 p r i n t f ( "%d \ n ", a+b ( c / d ) ) ; 10 p r i n t f ( "%d \ n ", ( a+b c ) / d ) ; 11 } 1 1