Files
Cours-C/ch004-structure.md

187 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Chapitre 4 — Structure dun programme C
## 4.1 Fonction `main`
Un programme C commence par une fonction `main`.
Cest le point dentrée du programme.
---
### Exemple
```c
int main(void)
{
return 0;
}
```
---
### Signification
- `int` → valeur de retour
- `main` → nom imposé
- `void` → pas darguments
---
### Variante avec arguments
```c
int main(int argc, char **argv)
{
return 0;
}
```
---
### Paramètres
- `argc` → nombre darguments
- `argv` → tableau de chaînes
---
## 4.2 Instructions et blocs
Un programme est composé dinstructions.
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 lobjet.
---
### 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
```