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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
int fin = 0;
do
{
char tab[8];
printf ("Entrer un entier de 5 digits maxi (\"\" pour quitter)\n");
fgets (tab, sizeof tab, stdin);
char *line_feed_found = strchr (tab, '\n');
if (!line_feed_found)
{
/* purge */
int c;
while ((c = getchar ()) != '\n' && c != EOF)
{
}
fprintf (stderr, "votre entrée fait plus de %d digits!!!\n",
(int) sizeof tab - 2);
}
else
{
*line_feed_found = 0;
if (*tab == 0)
{
fin = 1;
}
else
{
if (strlen (tab) > 5)
{
fprintf (stderr, "votre entrée fait plus de 5 digits!!!\n");
}
else
{
char *pend = NULL;
int nbr = (int) strtol (tab, &pend, 0);
if (*pend != 0)
{
fprintf (stderr, "ERROR: Conversion en entier raté!\n");
}
else
{
printf ("%d\n", nbr);
printf ("Float:%f\n", (double) nbr);
printf ("Hexa:%x\n", nbr);
printf ("Entier:%i\n", nbr);
}
}
}
}
}
while (!fin);
return 0;
} |