add ch4 : structure
This commit is contained in:
186
ch004-structure.md
Normal file
186
ch004-structure.md
Normal file
@@ -0,0 +1,186 @@
|
||||
# Chapitre 4 — Structure d’un programme C
|
||||
|
||||
## 4.1 Fonction `main`
|
||||
|
||||
Un programme C commence par une fonction `main`.
|
||||
|
||||
C’est le point d’entrée du programme.
|
||||
|
||||
---
|
||||
|
||||
### Exemple
|
||||
|
||||
```c
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Signification
|
||||
|
||||
- `int` → valeur de retour
|
||||
- `main` → nom imposé
|
||||
- `void` → pas d’arguments
|
||||
|
||||
---
|
||||
|
||||
### Variante avec arguments
|
||||
|
||||
```c
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Paramètres
|
||||
|
||||
- `argc` → nombre d’arguments
|
||||
- `argv` → tableau de chaînes
|
||||
|
||||
---
|
||||
|
||||
## 4.2 Instructions et blocs
|
||||
|
||||
Un programme est composé d’instructions.
|
||||
|
||||
Chaque instruction se termine par `;`.
|
||||
|
||||
---
|
||||
|
||||
### Exemple
|
||||
|
||||
```c
|
||||
int main(void)
|
||||
{
|
||||
int x = 5;
|
||||
x = x + 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Bloc
|
||||
|
||||
Un bloc est délimité par `{}`.
|
||||
|
||||
```c
|
||||
{
|
||||
int x = 10;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4.3 Commentaires
|
||||
|
||||
Les commentaires expliquent le code.
|
||||
|
||||
---
|
||||
|
||||
### Commentaire simple
|
||||
|
||||
```c
|
||||
// commentaire
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Commentaire multi-lignes
|
||||
|
||||
```c
|
||||
/*
|
||||
commentaire
|
||||
sur plusieurs lignes
|
||||
*/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4.4 Déclarations
|
||||
|
||||
Une déclaration annonce une variable ou fonction.
|
||||
|
||||
---
|
||||
|
||||
### Exemple
|
||||
|
||||
```c
|
||||
int x;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4.5 Définitions
|
||||
|
||||
Une définition crée réellement l’objet.
|
||||
|
||||
---
|
||||
|
||||
### Exemple
|
||||
|
||||
```c
|
||||
int x = 5;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4.6 Fichiers `.h` et `.c`
|
||||
|
||||
Le code est séparé en fichiers.
|
||||
|
||||
---
|
||||
|
||||
### `.c`
|
||||
|
||||
Contient le code.
|
||||
|
||||
---
|
||||
|
||||
### `.h`
|
||||
|
||||
Contient les déclarations.
|
||||
|
||||
---
|
||||
|
||||
### Exemple
|
||||
|
||||
```c
|
||||
// utils.h
|
||||
int add(int a, int b);
|
||||
```
|
||||
|
||||
```c
|
||||
// utils.c
|
||||
int add(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4.7 Organisation minimale
|
||||
|
||||
Un projet simple :
|
||||
|
||||
```
|
||||
main.c
|
||||
utils.c
|
||||
utils.h
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Compilation
|
||||
|
||||
```bash
|
||||
gcc main.c utils.c -o prog
|
||||
```
|
||||
Reference in New Issue
Block a user