Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours_prog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
yassin.elhakoun
cours_prog
Commits
a22a1f04
Verified
Commit
a22a1f04
authored
2 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
forgot to give this to students
parent
06f6d9e4
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/ligne_de_commande_c.md
+94
-0
94 additions, 0 deletions
slides/ligne_de_commande_c.md
with
94 additions
and
0 deletions
slides/ligne_de_commande_c.md
0 → 100644
+
94
−
0
View file @
a22a1f04
---
title
:
"
La
ligne
de
commande"
date
:
"
2022-11-29"
---
# La ligne de commande (1/4)
*
Ou comment passer des arguments à un programme en C.
## Point d'entrée d'un programme
-
Le point d'entrée est la fonction
`main()`
{.C}.
-
Elle peut être déclarée de 4 façon différentes:
1.
`void main()`
{.C}.
2.
`int main()`
{.C}.
3.
`void main(int argc, char **argv)`
{.C}.
4.
`int main(int argc, char *argv[])`
{.C}.
-
`argc`
{.C} est le nombre d'arguments passés à la ligne de commande:
**le premier est celui du programme lui-même**
.
-
`argv`
{.C} est un tableau de chaînes de caractères passés sur la ligne de commande.
# La ligne de commande (2/4)
## Exemple d'utilisation
Pour la fonction dans le programme
`prog`
```
C
int main(int argc, char **argv)
```
Pour l'exécution suivante on a
```
bash
$
./prog
-b
50 file.txt
```
```
C
argc == 4
argv[0] == "prog"
argv[1] == "-b"
argv[2] == "50"
argv[3] == "file.txt"
```
# La ligne de commande (3/4)
## Conversion des arguments
-
Les arguments sont toujours stockés comme des
**
chaînes de
caractères
**
.
-
Peu pratique si on veut manipuler des valeurs numériques.
-
Fonctions pour faire des conversions:
```C
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
double atof(const char *nptr);
int snprintf(char *str, size_t size,
const char *format, ...);
// str: buffer, size: taille en octets max à copier,
// format: cf printf(), ret: nombre de char lus
```
# La ligne de commande (4/4)
## Exemple d'utilisation
\f
ootnotesize
```
C
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
int main(int argc, char **argv) {
if (argc != 3) {
char *progname = basename(argv[0]);
fprintf(stderr, "usage: %s name age\n", progname);
return EXIT_FAILURE;
}
char *name = argv[1];
int age = atoi(argv[2]);
printf("Hello %s, you are %d years old.\n", name, age);
return EXIT_SUCCESS;
}
```
. . .
```
bash
$
./prog Paul 29
Hello Paul, you are 29 years old.
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment