🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fr.comp.lang.c
Article #15663

Re: Conversion int --> char

#15663
From: Emmanuel Delahaye
Date: Wed, 14 Sep 2005 16:53
84 lines
2596 bytes
Alain CROS wrote on 14/09/05 :
> La fonction getchar retourne un int que je peux stocker dans un char sans que 
> le compilateur ne se manifeste. Est ce toujours vrai <...> ?

A partir du moment ou tu as �limin� le cas 'EOF, oui.

> #include <stdio.h>
>
> int main(void)
>  {
>  char string[10];
>  char *pstr = string;
>  unsigned int i, j = 1;

Non! pour �valuer EOF, i doit �tre de type int. Tu l'as m�me rappel� :

"La fonction getchar retourne un int"

et puis 'i', c'est plut�t un index. Ici, on itilise plut�t 'c' pour 
'character'...

>  while((i = getchar()) != '\n' && j < 10)

Manque le test de EOF...

>   {
>   *pstr = i;
>   pstr++;
>   j++;

j est un compteur qui pourrait servir d'index (en demarrant de 0). Il 
est plus s�r d'utiliser cet index...

>   }
>  *pstr = '\0';
>  printf("\n%s\nj = %d\n", string, j);

'%d' attend un int. 'j' est de type unsigned int.

>  return 0;
> }

Ceci fonctionne :

#include <stdio.h>
#include <assert.h>

int main (void)
{
   char string[10] = "";
   unsigned int j = 0;
   int c;

   while ((c = getchar ()) != '\n'
    && c != EOF
    && j < sizeof string - 1)
   {
      string[j] = c;

      assert (string[sizeof string - 1] == 0);

      j++;
   }

      string[j] ='\0';

   assert (string[sizeof string - 1] == 0);
   printf ("\n%s\nj = %u\n", string, j);
   return 0;
}

azertyuiop

azertyuio
j = 9

-- 
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Technical Details
Message-ID: <mn.746d7d599f15346c.15512@YOURBRAnoos.fr>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!proxad.net!infeed-4.proxad.net!newsr4-1.free.fr!not-for-mail
References: <43285040$0$14548$636a55ce@news.free.fr>

Show Full Headers
Complete Article Headers
X-Google-Thread: f9822,2c3d52d9f1b4dc77
X-Google-Attributes: gidf9822,public
X-Google-Language: FRENCH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!proxad.net!infeed-4.proxad.net!newsr4-1.free.fr!not-for-mail
Subject: Re: Conversion int --> char
From: "Emmanuel Delahaye" <emdel@YOURBRAnoos.fr>
References: <43285040$0$14548$636a55ce@news.free.fr>
Newsgroups: fr.comp.lang.c
Message-ID: <mn.746d7d599f15346c.15512@YOURBRAnoos.fr>
X-Newsreader: MesNews/1.04.00.00-fr
Date: Wed, 14 Sep 2005 18:53:13 +0200
MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-15"; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 86
Organization: Guest of ProXad - France
NNTP-Posting-Date: 14 Sep 2005 18:53:14 MEST
NNTP-Posting-Host: 81.65.109.78
X-Trace: 1126716794 newsr4-1.free.fr 14569 81.65.109.78:18017
X-Complaints-To: abuse@proxad.net