-1

I'm trying to run this code, but it is not working.Can anyone please tell me what is wrong with this code After receiving the first input the program is terminating.

int main()
{
    char   name[100],college_name[100],address[100];
    printf("Enter your name :");
    scanf("%[^\n]s",name);
    printf("Enter college name :");
    scanf("%[^\n]s",college_name);
    printf("Enter address :");
    scanf("%[^\n]s",address);
}

1 Answers1

1

Your scanf calls are correctly reading everything up to — but not including — the \n.

The problem is that the \n remains on the input stream. So when the second and third scanf calls run, they find nothing. (They read zero characters up to but not including the next \n.)

The simplest fix would be to call getchar() to read and discard the \n characters:

#include <stdio.h>

int main()
{
    char name[100],college_name[100],address[100];
    printf("Enter your name :");
    scanf("%[^\n]",name);
    getchar();       /* discard \n */
    printf("Enter college name :");
    scanf("%[^\n]",college_name);
    getchar();       /* discard \n */
    printf("Enter address :");
    scanf("%[^\n]",address);
    getchar();       /* discard \n */
}

(I've also removed the extraneous s characters from the scanf calls. It's %[…], not %[…]s.)

Now, at one level this is a poor fix. Theoretically, we should check that the character read by getchar() was actually a \n. But (a) in this case it pretty much has to be, and (b) any program that uses scanf to read user input is already kludgey and unreliable, so adding some kludgey and potentially unreliable (but simple) getchar calls won't make it any worse.

(Some people will suggest -- one comment even suggested -- calling fflush(stdin) to get rid of the extraneous newlines. That might work on some platforms, but it's not guaranteed to and it won't work everywhere, so it's an even worse fix.)

The real lesson here is, don't use scanf for user input like this. It is, unfortunately, the obvious first choice for user input in beginning programs like this, because it seems so easy and convenient. But it turns out (when you try to make programs that actually work) scanf is actually just about impossibly inconvenient, so as soon as you've learned a little more about C, you'll want to abandon scanf entirely, and do all your input using more robust, reliable techniques. (Your next question is likely to be, more reliable techniques like what? One is to read lines of user input using fgets(), then manipulate those lines as necessary. See also http://c-faq.com/stdio/scanfprobs.html, https://www.eskimo.com/~scs/cclass/notes/sx10h.html, and https://www.eskimo.com/~scs/cclass/notes/sx12e.html.)


Addendum: I said, "The simplest fix would be to call getchar() to read and discard the \n characters." But there's an even simpler fix: change the second and third scanf calls (or all three of them, for consistency) to

scanf(" %[^\n]",address);

Notice the innocent-looking extra space. In any scanf call, a space in the format string means "read and discard whitespace characters". So on the second and third calls, that will take care of the \n that's leftover from the first and second calls.

Most scanf format specifiers (the ones like %d and %s and %f) skip leading whitespace automatically. But a few of them — notably %c and %[…] — do not. So for those, you typically want to include space character in the format string to cause the leftover \n from the preceding call to be skipped.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Also the `s` does not make sense here. `%[^\n]` already asks for all characters but a new-line. – alk Nov 25 '18 at 18:59
  • 1
    @alk Ah, right. I almost always miss that (in part because, following my own advice, I never use `scanf`). Answer edited. – Steve Summit Nov 26 '18 at 18:54