Documente Academic
Documente Profesional
Documente Cultură
paumier@univ-mlv.fr
Objectif du cours de C
apprendre le langage C analyser un problme pour crire le programme qui le rsout, de faon lgante et modulaire coder proprement savoir respecter un cahier des charges lors de projets tre au niveau pour le cours de systme en IR2
2
paumier@univ-mlv.fr
Planning
Slot 2:
paumier@univ-mlv.fr
les commandes systme de base l'criture et la compilation propre d'un programme C simple 2h de cours, 2h de TD, 4h de TP (1 par poste) TP not pour valuation
4
3 journes intensives:
dernire journe:
paumier@univ-mlv.fr
Linux
paumier@univ-mlv.fr
racine unique / priphriques vus comme des sousrpertoires. Ex: /mnt/floppy pour A:\ Hello.txt hello.txt HELLO.TXT
paumier@univ-mlv.fr
Compte utilisateur
paumier@univ-mlv.fr
Lister un rpertoire
commande ls
cogmu/ plp/ projet.html
paumier@univ-mlv.fr
paumier@univ-mlv.fr
Rpertoires
relatif:
absolu:
paumier@univ-mlv.fr
Rpertoires
crer un rpertoire
mkdir toto
dtruire un rpertoire
rmdir toto si toto est vide rm -ri toto sinon rm -rf toto pour viter de confirmer pour chaque fichier
paumier@univ-mlv.fr
Copie et renommage
copie de fichier:
cp toto ../titi
copie de rpertoire:
cp -r toto/ tutu
renommage ou dplacement:
mv toto tutu mv toto ../../test/toto.txt
paumier@univ-mlv.fr
12
Droits d'accs
-rwxrw-r-1 paumier igm rwx rw0 jun 22 11:02 toto r--
13
Droits d'accs
rpertoire R on peut voir le contenu r on peut lire F de R on peut crer des fichiers w on peut crire dans F ou des rpertoires ou le dtruire dans R on peut rentrer dans R x on peut excuter F si F est un excutable avec cd
Pour pouvoir crer un fichier titi/tutu, on doit avoir les droits w et x sur titi
fichier F
paumier@univ-mlv.fr
14
chmod u=wx toto : fixe wx les droits de l'utilisateur (user) pour le fichier toto, et laisse les autres inchangs:
g modifie les droits du groupe o modifie ceux des autres (others) a modifie tous les droits (all)
15
paumier@univ-mlv.fr
paumier@univ-mlv.fr
anjuta dans un terminal lance un diteur, mais la console n'a plus la main pour garder la main:
$>anjuta& [1] 2715
paumier@univ-mlv.fr
17
paumier@univ-mlv.fr
Tuer un programme
kill -3 PID : tue le programme kill -9 PID : tue le programme sauvagement (dernier recours) xkill : permet de slectionner une fentre
19
paumier@univ-mlv.fr
Afficher un fichier
cat toto: affiche le contenu de toto more toto: idem, avec dfilement si toto ne tient pas sur l'cran less toto: mieux que more (on peut remonter) head -n 17 toto: affiche les 17 premires lignes de toto tail -n 17 toto : affiche les 17 dernires lignes de toto
20
paumier@univ-mlv.fr
ls > toto : redirige la sortie standard de ls dans le fichier toto ls >> toto : idem, mais ajoute les donnes la fin de toto ls 2> toto : redirige la sortie d'erreur dans le fichier toto ls 2>> toto : idem, avec ajout en fin
paumier@univ-mlv.fr
21
2 paumier igm 4096 jun 22 17:13 titi/ 1 paumier igm 11 jun 22 17:13 tutu
>$ ls * > list 2> err >$ cat list tutu >$ cat err ls: titi: Permission denied
paumier@univ-mlv.fr
22
paumier@univ-mlv.fr
23
grep xxx *.txt : affiche toutes les lignes des fichiers *.txt qui contiennent xxx
>$ grep passwd ind* index:chpasswd (8) - update password file in batch index2:passwd [ssl-passwd] (1) - compute password hashes ind:rpc.yppasswdd [rpc] (8) - NIS password update daemon
paumier@univ-mlv.fr
24
Obtenir de l'aide
man xxx : donne la page de manuel de la commande xxx man -k xxx : donne les pages de manuel dont le titre contient xxx xxx --help : convention pour toutes les commandes du systme
paumier@univ-mlv.fr
25
26
Le langage C
C, parce qu'aprs le langage B :) 1972, Laboratoires Bell, Dennis Ritchie & Kenneth Thompson 1989: norme ANSI C langage impratif la fois haut et bas niveau programmation systme (noyau Linux)
paumier@univ-mlv.fr
27
Quelques rfrences
http://igm.univ-mlv.fr/~paumier/charte_des_projets.html
paumier@univ-mlv.fr
28
} } }
.c
.c
Compilation
.o .o libs
paumier@univ-mlv.fr
29
fichiers textes contenant les instructions extension spciale .c syntaxe trs prcise respecter sparateurs=espace+tab+retour la ligne programmation modulaire: un fichier par thme:
paumier@univ-mlv.fr
Le premier programme
Des commentaires
/* HelloWorld.c * Le classique des classiques */ #include <stdio.h> int main(int argc,char* argv[]) { printf("Hello world!\n"); return 0; }
paumier@univ-mlv.fr
31
Le premier programme
main est toujours la fonction principale d'un programme C
Retourne un entier
32
Le premier programme
Dbut de bloc d'instructions
/* HelloWorld.c * Le classique des classiques */ #include <stdio.h> int main(int argc,char* argv[]) { printf("Hello world!\n"); return 0; }
33
Le premier programme
compilation:
$>gcc HelloWorld.c -c $>gcc HelloWorld.o -o HelloWorld ou, en une tape: $>gcc HelloWorld.c -o HelloWorld
excution:
$>./HelloWorld Hello world!
34
paumier@univ-mlv.fr
Un autre exemple
Constante
#include <stdio.h> #define PI 3.14 float a,b; float sum(float x,float y) { return x+y; } int main(int argc,char* argv[]) { float c,d; a=PI; b=1; c=sum(a,b); printf("%f + %f = %f\n",a,b,c); printf("d=%f\n",d); return 0; }
paumier@univ-mlv.fr
f2.c
float sum(float x,float y) { return x+y; }
paumier@univ-mlv.fr
36
f2.c
float sum(float x,float y) { return x+y; }
f2.h
float sum(float x,float y);
paumier@univ-mlv.fr
Sources et headers
.c: code
on n'inclut pas les .c ! dfinitions des fonctions visibles de l'extrieur du .c dfinitions de constantes description de la bibliothque licence du code
.h:
paumier@univ-mlv.fr
38
Fichiers .h
paumier@univ-mlv.fr
39
Un beau .h
/* * Copyright (C) 2007 Sbastien Paumier <paumier@univ-mlv> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * ... * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * */ #ifndef ANGLES_H_ #define ANGLES_H_ /** * This library provides implementations of trigonometric functions. */ #define PI 3.14 float cosinus(float angle); float sinus(float angle); #endif
USA.
paumier@univ-mlv.fr
40
Options de compilation
$>gcc warnings.c $>gcc warnings.c -Wall -ansi Warnings.c:1: AVERTISSEMENT: Warnings.c: Dans la fonction Warnings.c:3: AVERTISSEMENT: Warnings.c:2: AVERTISSEMENT: Warnings.c:4: AVERTISSEMENT: fonction
le dfaut choisi du type retourn est int main : dclaration implicite de la fonction printf variable inutilise b contrle a atteint la fin non void de la
paumier@univ-mlv.fr
41
Messages d'erreurs
$>gcc -Wall -ansi cascade.c cascade.c:1: erreur d'analyse syntaxique avant b cascade.c: Dans la fonction sum : cascade.c:2: a non dclar (premire utilisation dans cette fonction) cascade.c:2: (Chaque identificateur non dclar est rapport une seule fois cascade.c:2: pour chaque fonction dans laquelle il apparat.) cascade.c:2: b non dclar (premire utilisation dans cette fonction)
paumier@univ-mlv.fr
42
instructions pour faciliter la compilation utilisables avec make nom = Makefile ou makefile
all: trigo trigo: main.o angles.o gcc main.o angles.o -o trigo @echo Compilation finie. clean: rm -f trigo main.o angles.o %.o: %.c gcc -c -Wall -ansi $<
43
cible: dpendances
TAB TAB
action1 action2
...
paumier@univ-mlv.fr
all: trigo trigo: main.o angles.o gcc main.o angles.o -o trigo @echo Compilation finie. clean: rm -f trigo main.o angles.o
paumier@univ-mlv.fr
44
dtection des modifications base sur les dates de fichiers make ne reconstruit que le ncessaire
$> make gcc -c -Wall -ansi main.c gcc -c -Wall -ansi angles.c gcc main.o angles.o -o trigo Compilation finie. $> make make: Rien faire pour all .
45
paumier@univ-mlv.fr
Propret du code
1) indent proprement (utiliser indent si ncessaire)
int { foo( void) int foo(void) { int i,j=0,u; for (i=0;i<10;i++) { j=j+i; u=u+j; } return u; }
int i, j=0;
paumier@univ-mlv.fr
46
Propret du code
2) comment judicieusement
on ne commente pas la syntaxe!
/* Affiche un tableau pass en paramtre */ void affiche_tableau(int tableau[],int taille) { int i; /* dclaration d'une variable i */ for (i=0;i<taille;i++) { /* on parcourt le tableau */ printf("%d ",tableau[i]); /* en affichant chaque case */ } } /* Alloue et retourne un tableau de 'taille' entiers, ou NULL en * cas d'erreur. Chaque case d'indice i du tableau est initialise * avec f(i), o f est une fonction passe en argument de f_alloc. */ int* f_alloc(int taille,int (*f)(int)) { int* t=(int*)malloc(taille*sizeof(int)); if (t==NULL) return NULL; int i; for (i=0;i<taille;i++) t[i]=f(i); return t; }
paumier@univ-mlv.fr
47
Propret du code
3) des identificateurs judicieux
a f(a b,c d) { if (b==NULL) return NULL; if (b->e==d) return b; return f(b->s,d); }
ColorList find_color(ColorList l,Color c) { if (l==NULL) return NULL; if (l->color==c) return l; return find_color(l->next,c); }
paumier@univ-mlv.fr
48
Propret du code
4) du code lisible par tous (mondialisation oblige...)
listacores achacor(listacores l,cor c) { if (l==NULL) return NULL; if (l->cor==c) return l; return achacor(l->proxima,d); } ColorList find_color(ColorList l,Color c) { if (l==NULL) return NULL; if (l->color==c) return l; return find_color(l->next,c); }
paumier@univ-mlv.fr
49
Propret du code
5) viter le style gourou
i,j;main(n){for(;i<1E4;j||printf("%d ",n,++i))for(j=n++;n%j--;);)}
paumier@univ-mlv.fr
50
Propret du code
6) cohrence de prsentation avec:
/* This function looks for the given color in the given color list. */ ColorList findColor(ColorList l, Color c) { if (l==NULL) { return NULL; } if (l->color==c) { return l; } return findColor(l->next,c); }
/** * This function looks for the given * color in the given color list. */ ColorList find_color(ColorList l,Color c) { if (l==NULL) {return NULL;} if (l->color == c) {return l;} return find_color(l->next,c); }
/* This function looks for the given color */ /* in the given color list. */ ColorList find_Color(ColorList l , Color c) { if (l == NULL) return NULL ; if (l->color == c) return l ; return find_Color(l->next , c) ; }
paumier@univ-mlv.fr
51