Professional Documents
Culture Documents
2
Fonction
principale Exemple1
Entre du Fichier en tte
programme
#include <stdio.h>
// Ceci est un commentaire
Instruction
int main() daffichage
{
printf( "Bonjour !\n");
return 0;
Dbut
bloc }
Fin bloc
3
Exemple 2
#include <stdio.h> Dclaration de variables
locales main()
void main()
{
int a, b, c;
printf( "Entrez deux chiffres : "); Saisie de la valeur de a
scanf("%d",&a);
scanf("%d",&b);
Calculer la somme
c=a+b;
printf( "c =%d ", c);
printf( "\n Fin de traitement...\n\n");
}
4
lments lexicaux
Les mots (lexmes) reconnues par le
langage C sont regroups en six classes :
1. Mots cl (keyword)
2. Identificateur
3. Constante
4. Chane littrale
5. Oprateur
6. sparateur
5
les sparateurs
6
les sparateurs (2)
La virgule spare les lments d'une liste de
valeurs , d'argument de fonction, ou d'expression
Le Point virgule marque la fin d'une dclaration,
ou d'une expression.
Le signe gal (=) spare les dclarations de
variables des valeurs d'initialisation :
int x=5;
7
Les mots cls
Les mots-cls sont des mots rservs du langage pour des
buts spciaux et ne doivent pas tre employs comme des
identificateurs.
char; double ; enum ; float ; int ; long ; short ;
signed ; struct ; union ; unsigned ; wchar_t ;
__except; __finally; __try; break ; case ; catch;
continue ; default ; do ; else ; for ; goto; if;
return ; switch ; throw ; try ; while ;
free; malloc; operator; sizeof; typeid
auto; extern; mutable; register; static; typedef
8
Les constantes de type caractre
9
Les constantes de type caractre (2)
\a
\a signal
signal sonore;
sonore;
\b
\b Retour
Retour en
en arrire
arrire (Backspace)
(Backspace) ;;
\f
\f Saut
Saut de
de page
page (Formfeed);
(Formfeed);
\n
\n Nouvelle
Nouvelle ligne
ligne (linefeed)
(linefeed) ;;
\r
\r Retour
Retour chariot
chariot (Carriage
(Carriage return);
return);
\t
\t Tabulation
Tabulation horizontale;
horizontale;
\v
\v Tabulation
Tabulation Verticale
Verticale ;;
\\
\\ Antislash
Antislash (Backslash);
(Backslash);
\'
\' apostrophe;
apostrophe;
\"
\" Double
Double quote
quote
10
Les chane littrales
"Bonjour."
"Aujourd\'hui"
11
Types de base
13
Type boolen
14
Variables simples
typeDeLaVariable nomDeLaVariable
int a, b, c;
15
Oprateurs
Arithmtiques Relationnels
+addition >>=<=<comparaisons
soustraction ==!=galit et ingalit
*multiplication !ngation (oprateur
unaire)
/division (entire ou
relle) &&ET relationnel
%modulo (sur les entiers) ||OU relationnel
=affectation ++incrmentation
dcrmentation
16
Oprateurs
Combins, de la forme
<variable><op>=<expr>
o <op> est un oprateur. Une telle expression est quivalente
lexpression :
<variable> =<variable><op><expr>
+=x+=yquivaut x=x+y;
=x=yquivaut x=xy;
17
Instructions de base : Lecture
Cette instruction permet dintroduire une
valeur dune variable partir du clavier ou
un organe dentre
En langage C, on utilise :
scanf (format, liste_dadresses)
Scanf("%d",&x) ;
Le tableau suivant prsente les diffrents formats
existants :
18
Format Type de la Ivalue correspondant
c Char
d Int
hd short int
hu unsigned short
ld long int
lu unsigned long
f Float
lf Double
s Chane de caractres
u unsigned int 19
Instructions de base : criture
Cette instruction permet dafficher ou
dcrire la valeur dune constante, dune
variable ou le rsultat dune expression
dans un organe de sortie
En C on utilise :
printf (format, liste_dexpressions)
printf("a= %f",a)
20
La structure : Si alors
C
Algorithme
if (Condition)
Si (Condition) instruction ;
alors
traitement
if (Condition) {
Fsi
instruction_1 ;
instruction_2 ;
instruction_n;
}
21
La structure : Si alors
C
if (Condition)
max
max=a=a;; instruction ;
ifif(max<b)
(max<b) max=
max=bb;;
if (Condition) {
ifif(a<b)
(a<b) {{ instruction_1 ;
instruction_2 ;
c=a
c=a;;
a=b
a=b;; instruction_n;
b=c
b=c ;; }
}}
22
La structure :Si.alorssinon
C
Algorithme if (Condition)
Si (Condition) instruction1 ;
alors else
traitement1 instruction2 ;
sinon if (Condition)
traitement2 {
instruction_1_1 ;
Fsi
instruction_1_n ;
}
else {
instruction_2_1 ;
instruction_2_k ;
} 23
La structure :Si.alorssinon
ifif(x<
(x<0)
0) C
abs
abs==-x
-x;; if (Condition)
else
else instruction1 ;
abs
abs=x
=x;; else
instruction2 ;
ifif(x<0) if (Condition)
(x<0) {{
abs {
abs==-x -x;; instruction_1_1 ;
printf(%f"
printf(%f",,abs
abs););
}} instruction_1_n ;
else
else {{ }
abs
abs=x=x;; else {
printf(%f"
printf(%f",,abs
abs);); instruction_2_1 ;
}}
instruction_2_k ;
} 24
Expressions conditionnelles
expr1? expr2 : expr3
est value de la manire suivante :
si expr1 alors expr2
sinon expr3
fsi
z = (a > b) ? a : b;
25
Choix multiples
26
La structure : TANT QUE
do
do{{
do { printf
printf("("donner
donneruneunevaleur
valeur")");;
instruction_1 ; scanf
scanf ("("%d,
%d,&A)
&A) ;;
instruction_2 ; i++
i++;;
}}while
while((i<10)
i<10);;
instruction_n;
} while (condition) ;
29