No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Compter les caractères...

Ce programme fait suite au programme copy1.c, et permet de compter le nombre de caractères lus sur le canal d'entrée standard.

ComptCar.c
#include <stdio.h>

void main (void)
{
long nc; /*nbre de caracteres*/
nc=0;
while (getchar()!=EOF)
{
++nc;
printf("%ld\n", nc);
}
}

Résultat

La séquence entrée est la suivante:

Je compte les lettres [Enter]

Ce qui provoque l'affichage suivant:

Je compte les lettres
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-

Remarques

  • Nous pouvons remarquer l'opérateur de pré incrémentation dans l'expression "++nc".
    Cela signifie que la valeur de la variable nc est augmentée d'une unité avant n'importe quel traitement.
    L'expression "nc++" permet d'augmenter d'une unité la valeur de la variable nc après son traitement.
  • Comme nous travaillons avec une variable de type long, "%ld
    " indique à la fonction printf que l'argument correspondant est de type long.

Compter les lignes...

ComptLign.c
#include <stdio.h>

void main (void)
{
int c, nl;
nl=0;
while ((c=getchar())!=EOF)
{
if (c=='\n')
{
++nl;
printf("%d\n", nl);
}
}
}

Résultat

La séquence entrée est la suivante:

Je[Enter]compte[Enter]les[Enter]lignes[Enter]

Ce qui provoque l'affichage suivant:

Je
1
compte
2
les
3
lignes
4
-

Attention

Si nous oublions les apostrophes qui délimitent les instructions de la condition if, cela provoque un affichage désastreux...

ComptLign.c
#include <stdio.h>

void main (void)
{
int c, nl;
nl=0;
while ((c=getchar())!=EOF)
{
if (c=='\n')
++nl;
printf("%d\n", nl);
}
}


Je
0
0
1
compte
1
1
1
1
1
1
2
les
2
2
2
3
lignes
3
3
3
3
3
3
4
-

Tout compter...

Le programme suivant nous propose de compter les lignes, les mots, et les caractères.

ComptTout.c
#include <stdio.h>

#define OUI 1
#define NON 0

void main (void)
{
int c, nl,nm,nc,mot;
mot=NON;
nl=nm,nc=0;
while ((c=getchar())!=EOF){
++nc;
if (c=='\n')
++nl;
if (c==' '||c=='\n'||c=='\t')
mot=NON;
else if (mot==NON)
{
mot=OUI;
++nm;
}
}
printf("%d lignes.\n%d mots.\n%d caracteres.\n", nl, nm, nc);
}

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 02/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/c-compteur.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.