Coder's Guild Mailing List

Re: Using getchar()

Posted by -=goda=- on 1999-09-29

This works:

#include <stdio.h>

void main()
{
   char c[50];
   int i;

   printf("please enter the name: ");
   for (i=0;;i++)
   {
    c[i]=getchar();
    if (c[i]=='\n') break;
   }
   c[i]=0;
   printf("your name is %s and has %d characters",c,i);
}

David / Remorseless Games
http://remorseless.homepage.com

----- Original Message -----
From: <tcg@xxxx.xx.xx>
To: <goda@xxx.xxx>
Sent: Tuesday, September 28, 1999 6:07 PM
Subject: TCG digest 595


> Date: Tue, 28 Sep 1999 21:22:25 +0800
> From: "Hasan" <kacmaz@xxxxx.xxxxx.xxx.xx>
> Subject: Using getchar() instead of fgets or scanf
>
> It has been told always that usage of fgets or scanf is not an efficient
way
> to read keyboard input streams. I thought about using the following code
but
> even though it complies ok, it does not run? can anyone help? or anyother
> suggestions instead of this code...?
>
> thx
>
>
> #include <stdio.h>
> void main()
> {
>  char c;
>    int i=0;
>
>    printf("please enter the name");
>    for (c=getchar();c!='\n';c=getchar())
>    {
>     i+=1;
>    }
>    printf("your name is %s and has %d characters",c,i);
> }
>