开发者

Character trouble in C

I'm a beginner to C and I'm having trouble understanding why this loop isn't reading my character datatype. What's wrong with my code?

  #include <stdio.h>
  #include <stdlib.h>

int main()
{
    /************
     Declaration
    ************/

    int admin = 0; // Counts and administrates the loop
    char开发者_StackOverflow中文版 userInput; // Accounts for the name entered

    printf("Enter your  first name to the screen :");
    scanf("%c", &userInput);

    while (admin < 100)
    {
          printf("Hello, my name is %c!\n", userInput);
          admin++;
    }

    system("pause");
    return 0;


char userName[100];

and

scanf("%99s", userName);

and

printf("Hello, my name is %s!\n", userName);

Unless your username is one character long!

(in C a char is a single char. Clearly you want a C "string", so an array of chars (or you could malloc a piece of memory, but we will ignore that). So you declare an array of chars (I put a length of 100) but then you have to use %s with scanf and printf. As a sidenote you don't need the & to take the address of an array)

Someone else (Richard) has put %99s instead of %s because this prevents scanf from reading in names that are longer than 99 characters. Doing this would cause serious problems! He was VERY right! And I thank him. :-)

I'll add that I hope you know that a C string is "zero terminated" (the last character must be a \0) (a 0). For this reason an array of 100 chars can contain only a string long 99 (because the 0 terminator isn't counted).


The question you have asked i also faced it few months back. what i knew i am telling you.

when you use %c for scanf() it is able to take any 1 character input value. whether it is a number, an alphabet or your last pressed 'enter key' which is \n . It can take buffer values too. So in this case i different from scanf() and %d in which scanf() waits for a number and dont accepts an alphabet. But for scanf() and %c it can accept even the buffer values too. So to overcome this problem you can use these three methods

1) you can declare a inbuilt function fflush(stdin); just before scanf(). like this

           printf("Enter your  first name to the screen :");
           fflush(stdin);
           scanf("%c", &userInput);

then your program will work fine. And dont forget to include the <stdlib.h> header file. this function is contained in that .Now i tell you demerits of this method. i have read at many places that this approach reduces the portability of the program so ANSI dont recommend this way.

2) you can declare an character array instead of a character variable. then scanf(). it also works fine.

3) this one is simplest. when you write statement like this scanf("%c", &userInput); I suggest you to write the statement like this scanf(" %c", &userInput); So can you detect the difference?? Actually an space is added just before the %c and now program works.

You can try all three methods and if any one of those doesnt work please notify me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜