-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaidememoirec.lyx
4275 lines (3159 loc) · 72.3 KB
/
aidememoirec.lyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass scrartcl
\use_default_options true
\begin_modules
fix-cm
logicalmkup
\end_modules
\maintain_unincluded_children false
\language french
\language_package auto
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize a4paper
\use_geometry true
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date true
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\leftmargin 2.5cm
\topmargin 2cm
\rightmargin 2.5cm
\bottommargin 2.5cm
\footskip 1cm
\secnumdepth 3
\tocdepth 3
\paragraph_separation skip
\defskip medskip
\quotes_language french
\papercolumns 1
\papersides 1
\paperpagestyle plain
\bullet 0 5 2 -1
\bullet 1 0 26 -1
\bullet 2 0 12 -1
\bullet 3 0 8 -1
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Title
\family roman
\shape smallcaps
Aide Mémoire - Langage C/C++
\end_layout
\begin_layout Author
Philippe Roussel
\begin_inset Newline newline
\end_inset
\size small
version 0.5
\end_layout
\begin_layout Standard
\begin_inset CommandInset toc
LatexCommand tableofcontents
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status collapsed
\begin_layout Plain Layout
\backslash
renewcommand
\backslash
labelitemi{
\backslash
textbullet}
\end_layout
\begin_layout Plain Layout
\backslash
renewcommand
\backslash
labelitemii{
\backslash
normalfont
\backslash
bfseries
\backslash
textendash}
\end_layout
\begin_layout Plain Layout
\backslash
renewcommand
\backslash
labelitemiii{
\backslash
textasteriskcentered}
\end_layout
\begin_layout Plain Layout
\backslash
renewcommand
\backslash
labelitemiv{
\backslash
textperiodcentered}
\end_layout
\end_inset
\end_layout
\begin_layout Section
Directives du préprocesseur
\end_layout
\begin_layout Standard
Le préprocesseur et ses directives, qui commencent toutes par
\emph on
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\emph on
#
\end_layout
\end_inset
\emph default
en début de ligne, ne font pas à proprement parler partie du langage C.
Ce sont des outils permettant de manipuler le code source vu en tant que
fichier texte avant qu'il soit analysé par le compilateur.
\end_layout
\begin_layout Standard
Les directives peuvent être classées en plusieurs catégories :
\end_layout
\begin_layout Subsection
Inclusion de fichiers
\end_layout
\begin_layout LyX-Code
#include <nom_de_fichier> /* pour les fichiers systèmes */
\end_layout
\begin_layout LyX-Code
#include "nom_de_fichier" /* pour les fichiers locaux */
\end_layout
\begin_layout Standard
Par exemple :
\end_layout
\begin_layout LyX-Code
#include <stdio.h> /* inclusion de la bibliothèque stdio */
\end_layout
\begin_layout LyX-Code
#include "adresse.h"
\end_layout
\begin_layout Subsection
Définition de macros
\end_layout
\begin_layout Standard
Une macro créée par la directive
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#define
\end_layout
\end_inset
indique au préprocesseur qu'il doit remplacer une chaîne de caractères
par une autre, sans analyser la signification de ces chaînes.
Il est possible de créer des macros avec ou sans argument comme suit :
\end_layout
\begin_layout LyX-Code
#define nom valeur
\end_layout
\begin_layout LyX-Code
#define PI 3.14
\end_layout
\begin_layout LyX-Code
#define carre(x) ((x)*(x))
\end_layout
\begin_layout LyX-Code
#define somme(a, b) ((a)+(b))
\end_layout
\begin_layout Standard
Les macros ci-dessus définies, le code C suivant :
\end_layout
\begin_layout LyX-Code
printf("%f %d %d", PI, carre(4+1), somme(3-1, 8));
\end_layout
\begin_layout Standard
sera traduit ainsi par le préprocesseur :
\end_layout
\begin_layout LyX-Code
printf("%f %d %d", 3.14, ((4+1)*(4+1)), ((3-1)+(8)));
\end_layout
\begin_layout Standard
Une macro peut être annulée par la directive
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#undef
\end_layout
\end_inset
.
Par exemple :
\end_layout
\begin_layout LyX-Code
#undef carre
\end_layout
\begin_layout Standard
\series bold
Attention aux effets de bord :
\series default
l'utilisation de macros peut
\begin_inset Quotes fld
\end_inset
cacher
\begin_inset Quotes frd
\end_inset
le code réellement exécuté et rendre difficile l'inspection du code.
Ainsi la macro
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\family typewriter
carre(x)
\end_layout
\end_inset
définie plus haut référence 2 fois son argument.
Celui-ci sera donc évalué 2 fois, ce qui peut avoir des effets dévastateurs.
Par exemple le code suivant :
\end_layout
\begin_layout LyX-Code
\family typewriter
x = 4;
\end_layout
\begin_layout LyX-Code
\family typewriter
y = carre(x++);
\end_layout
\begin_layout Standard
sera traduit ainsi :
\end_layout
\begin_layout LyX-Code
x = 4;
\end_layout
\begin_layout LyX-Code
y = ((x++)*(x++));
\end_layout
\begin_layout Standard
ce qui n'est pas vraiment le comportement attendu : une fois les deux lignes
exécutées, x vaudra 6 au lieu des 5 attendus et y 20 eu lieu de 16.
Le problème n'existerait pas en utilisant une fonction
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
carre
\end_layout
\end_inset
définie ainsi :
\end_layout
\begin_layout LyX-Code
int carre(int x)
\end_layout
\begin_layout LyX-Code
{
\end_layout
\begin_layout LyX-Code
\paragraph_spacing single
\noindent
return x * x;
\end_layout
\begin_layout LyX-Code
}
\end_layout
\begin_layout Standard
Dans ce cas, le paramètre n'est évalué qu'une fois lors de l'appel de la
fonction.
\end_layout
\begin_layout Subsection
Compilation conditionnelle
\end_layout
\begin_layout Standard
Les directives
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#if
\end_layout
\end_inset
,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#ifdef
\end_layout
\end_inset
,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#ifndef
\end_layout
\end_inset
,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#else
\end_layout
\end_inset
,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#elif
\end_layout
\end_inset
et
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
#endif
\end_layout
\end_inset
permettent de conditionner la compilation de certaines parties du code.
Par exemple :
\end_layout
\begin_layout LyX-Code
#if 0 /* 0 est équivalent à Faux, les autres valeurs valant Vrai */
\end_layout
\begin_layout LyX-Code
printf("Ces lignes ne seront pas compilées car la condition");
\end_layout
\begin_layout LyX-Code
printf("est fausse : une valeur numérique est vraie si et");
\end_layout
\begin_layout LyX-Code
printf("seulement si elle est différente de 0");
\end_layout
\begin_layout LyX-Code
#endif
\begin_inset Newline newline
\end_inset
\end_layout
\begin_layout LyX-Code
#ifdef DEBUG
\end_layout
\begin_layout LyX-Code
printf("Ces deux lignes seront compilées si DEBUG existe");
\end_layout
\begin_layout LyX-Code
printf("c'est-à-dire si un #define DEBUG apparaît avant");
\end_layout
\begin_layout LyX-Code
#else
\end_layout
\begin_layout LyX-Code
printf("Si DEBUG n'existe pas cette ligne est compilée");
\end_layout
\begin_layout LyX-Code
#endif
\end_layout
\begin_layout Section
Commentaires
\end_layout
\begin_layout Standard
Les commentaires doivent soit se trouver après le marqueur
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
//
\end_layout
\end_inset
soit à l'intérieur d'un bloc de commentaire délimité par
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
/* ...
*/
\end_layout
\end_inset
.
Par exemple :
\end_layout
\begin_layout LyX-Code
// Ceci est un commentaire sur une ligne
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
/* Il peut également être écrit de cette façon */
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
/*
\end_layout
\begin_layout LyX-Code
* Pour les commentaires nécessitant plusieurs lignes
\end_layout
\begin_layout LyX-Code
* ceci est le format généralement recommandé
\end_layout
\begin_layout LyX-Code
*/
\end_layout
\begin_layout Section
Structure d'un programme C
\end_layout
\begin_layout Standard
Un programme C se structure généralement de la façon suivante :
\end_layout
\begin_layout LyX-Code
/* Les inclusions de bibliothèques */
\end_layout
\begin_layout LyX-Code
#include <stdio.h>
\end_layout
\begin_layout LyX-Code
#include <stdlib.h>
\begin_inset Newline newline
\end_inset
\begin_inset Newline newline
\end_inset
/* Les définitions des constantes */
\end_layout
\begin_layout LyX-Code
#define TAILLE_MAX 200
\end_layout
\begin_layout LyX-Code
\end_layout
\begin_layout LyX-Code
/* Les variables globales et les prototypes de fonctions */
\end_layout
\begin_layout LyX-Code
int compteur;
\end_layout
\begin_layout LyX-Code
int tableau[TAILLE_MAX];
\begin_inset Newline newline
\end_inset
\begin_inset Newline newline
\end_inset
int nombre_zero(int, int *);
\begin_inset Newline newline
\end_inset
\begin_inset Newline newline
\end_inset
/* La fonction main appelée lors de l'exécution */
\begin_inset Newline newline
\end_inset
int main(int argc, char *argv[])
\end_layout
\begin_layout LyX-Code
{
\end_layout
\begin_layout LyX-Code
...
\end_layout
\begin_layout LyX-Code
return 0;
\end_layout
\begin_layout LyX-Code
}
\begin_inset Newline newline
\end_inset
\begin_inset Newline newline
\end_inset
/* Le reste des fonctions */
\end_layout
\begin_layout LyX-Code
int nombre_zero(int taille, int *tableau)
\end_layout
\begin_layout LyX-Code
{
\end_layout
\begin_layout LyX-Code
int nb = 0;
\end_layout
\begin_layout LyX-Code
...
\end_layout
\begin_layout LyX-Code
return nb;
\end_layout
\begin_layout LyX-Code
}
\begin_inset Newline newline
\end_inset
/* etc */
\end_layout
\begin_layout Section
La fonction
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
main
\end_layout
\end_inset
\end_layout
\begin_layout Standard
La fonction
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
main
\end_layout
\end_inset
, qui est la première fonction exécutée lors de l'exécution du programme,
peut être déclarée avec plusieurs prototypes différents :
\end_layout
\begin_layout LyX-Code
void main(void);
\end_layout
\begin_layout LyX-Code
/* ou plus couramment */
\end_layout
\begin_layout LyX-Code
int main(int argc, char *argv[]);
\end_layout
\begin_layout LyX-Code
/* ou plus rarement */
\end_layout
\begin_layout LyX-Code
int main(int argc, char *argv[], char *env[]);
\end_layout
\begin_layout Standard
Dans la seconde variante,
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
argc
\end_layout
\end_inset
représente le nombre d'éléments présents dans la tableau de chaînes de
caractères
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
argv
\end_layout
\end_inset
.
Ce tableau est initialisé par le système lors du lancement du programme
: le premier élément contient le nom du programme exécuté et les éléments
suivants les éventuels paramètres passés au programme.
Par exemple, si j'exécute le programme
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
copiefichier.exe
\end_layout
\end_inset
de la façon suivante dans une fenêtre dos :
\end_layout
\begin_layout LyX-Code
copiefichier.exe fichier_origine.txt fichier_destination.txt
\end_layout
\begin_layout Standard
la fonction
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
main
\end_layout
\end_inset
sera appelée avec
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
argc = 3
\end_layout
\end_inset
et le tableau
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
argv
\end_layout
\end_inset
suivant :
\end_layout
\begin_layout LyX-Code
argv[0] = "copiefichier.exe"
\end_layout
\begin_layout LyX-Code
argv[1] = "fichier_origine.txt"
\end_layout
\begin_layout LyX-Code
argv[2] = "fichier_destination.txt"
\end_layout
\begin_layout Section
Variables et types
\end_layout
\begin_layout Standard
La langage C est dit
\emph on
statiquement typé,
\emph default
ce qui signifie que le type de chaque variable est défini explicitement
par le programme et que ce type ne peut pas être modifié durant l'exécution.
\end_layout
\begin_layout Subsection
\begin_inset CommandInset label
LatexCommand label
name "sub:types-entiers"
\end_inset
Types entiers : char, short, int, etc
\end_layout
\begin_layout Standard
La différence entre ces types est uniquement sur le nombre d’octets de codage
en mémoire :
\end_layout
\begin_layout Labeling
\labelwidthstring 00.00.0000
\series medium
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\series medium
char
\end_layout
\end_inset
\series default
entier sur 1 octet (-128 à 127).
Également utilisé pour stocker les caractères ASCII
\end_layout
\begin_layout Labeling
\labelwidthstring 00.00.0000
\series medium
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\series medium
short
\end_layout
\end_inset
\series default
entier sur 2 octets (-32768 à 32767)
\end_layout
\begin_layout Labeling
\labelwidthstring 00.00.0000
\series medium
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\series medium
int
\end_layout
\end_inset
\series default
entier sur 2 ou 4 octets suivant le compilateur et l'architecture
\end_layout
\begin_layout Labeling
\labelwidthstring 00.00.0000
\series medium
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\series medium
long
\begin_inset space ~
\end_inset
int
\end_layout
\end_inset
\series default
entier sur 4 octets (-2 147 483 648 à 2 147 483 647)
\end_layout
\begin_layout Labeling
\labelwidthstring 00.00.0000
\series medium
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\series medium
long
\end_layout
\end_inset
\series default
raccourci pour
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
long int
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Ces types entiers peuvent tous être précédés du mot-clé
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
\begin_inset Flex Strong
status collapsed
\begin_layout Plain Layout
unsigned
\end_layout
\end_inset
\end_layout
\end_inset
pour les transformer en entiers non signés :
\end_layout
\begin_layout Labeling
\labelwidthstring 00.00.0000
\begin_inset Flex Code
status collapsed
\begin_layout Plain Layout
unsigned
\begin_inset space ~
\end_inset
char