scanf in C : Reading single line of text using "scanf" function

I have done it 4-5 years ago, that to read a whole line of text (including space) using scanf function.

When I was preparing for my exams, I went back to the old days of a “C” student. Nowadays I’m a bit used with C++. So I forgot the format specifier to specfy inside the scanf function. Finally my memory retrieved it back

Here’s it is:

char name[100];

printf(“Enter the name:”);
scanf(“%[^\n]“,name);

Something cool no?

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

19 Comments

  1. Posted November 1, 2006 at 12:20 pm | Permalink

    You have to make sure that:
    name[99] = ”;

  2. Posted November 3, 2007 at 7:41 pm | Permalink

    It’s vulnerable to buffer overflows :(

  3. Jeffrey
    Posted February 19, 2008 at 11:35 am | Permalink

    Use fflush(stdin); to avoid looping!?

  4. Posted March 8, 2008 at 3:58 am | Permalink

    THANK YOU. Ive been searching forever online for this!!!!!!!!!!!! you rock

  5. Aia
    Posted August 26, 2008 at 1:45 am | Permalink

    >It’s vulnerable to buffer overflows
    To avoid that declare a maximum field
    if ( scanf(“%99[^\n]“, name) == 1) { /* do something */} will do it. However does leave still input in the stdin buffer if more than 99 keys where typed.

    >Use fflush(stdin); to avoid looping!?
    That’s invoking undefined behavior.
    fflush() is only for OUTPUT streams, and not for INPUT like stdin.

  6. sd
    Posted September 22, 2008 at 3:55 pm | Permalink

    Hey thanks a lot for that piece of code! Works great!
    The strange thing is that the string variable doesn’t need a preceeding ampersand (&).
    Quite surprising….I’d like to know how that happens…

  7. Luis Tellez
    Posted December 2, 2008 at 11:23 pm | Permalink

    fflush(stdin) works fine for the input, i have use it in many proyects i haver worked and havent given me any problems.

  8. ashih
    Posted January 28, 2009 at 5:23 pm | Permalink

    thnx dear….

  9. jessica
    Posted February 2, 2009 at 1:23 pm | Permalink

    you are awsom

  10. Dan D.
    Posted February 9, 2009 at 5:54 am | Permalink

    I wish I’d found this blog posting two hours ago.

  11. Shailesh
    Posted April 4, 2009 at 12:07 am | Permalink

    Awesome. This works great

  12. Will
    Posted April 7, 2009 at 1:56 pm | Permalink

    Use fgets instead…

    The version that is not vulnerable to buffer overflows is:

    char name[100];

    printf(”Enter the name:”);
    fgets(name,100,stdin);

    Hope that helps…

  13. diapir
    Posted June 1, 2009 at 1:29 pm | Permalink

    Cool reminder. I used this thing to skip comments in a file :

    // eat spaces
    // match ‘#’
    // capture and discard everything until end of line
    fscanf(stream, ” #%*[^\n]“);

    Cheers

  14. sub
    Posted July 6, 2009 at 4:32 pm | Permalink

    i didn’t get tis…. none of the codes worked for me,including the one containing regular expressions.
    how do we read a line from the user in C.

  15. ullas
    Posted July 7, 2009 at 9:30 am | Permalink

    Hey this is not working…..

  16. Bubba
    Posted July 30, 2009 at 12:51 pm | Permalink

    (To Sub)Two questions back:

    Here is a simple snippet to answer your question:

    #include

    int main(int argc, char *argv[])
    {
    char first_name[100];

    printf(“What is your name? “);
    gets(first_name);
    printf(“\nNice to meet you %s!”,first_name);
    return 0;
    }

    Try this, I hope it helps in some small way…..

  17. Bubba
    Posted July 30, 2009 at 12:53 pm | Permalink

    Oddly,
    The post deleted what was in the brackets after include (which was simply “stdio.h”)

  18. Bubba
    Posted July 30, 2009 at 8:36 pm | Permalink

    Or, we can just use

    #include
    int main()
    {
    char name[99];
    printf(“What is your name? “);
    scanf(“%s”,&name);
    printf(“Hello, %s. How are you?\n”,name);
    return(0);
    }

    Same result.

  19. Posted August 29, 2009 at 1:10 pm | Permalink

    I would like to see more blog entries like this one

2 Trackbacks

  1. [...] (what’s surprising is that abc doesn’t need an ampersand preceeding it) Got it from here and there are comments about scanf being vulnerable to buffer overflows. Is that true? So [...]

  2. By Anonymous on May 17, 2009 at 3:58 pm

    [...] Datei lesen und an bestimmer Stelle verändern Am besten ist vielleicht, immer ein komplette Zeile einzulesen. Dann musst du immer prüfen, ob du nun an dem zu editierenden Block angekommen bist. Das kann man [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>