scanf or gets?

This is a discussion on scanf or gets? within the C Programming forums, part of the General Programming Boards category; I have been reading through a ton of tutorials trying to learn C programming. I have read in some places ...

  1. #1
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127

    scanf or gets?

    I have been reading through a ton of tutorials trying to learn C programming. I have read in some places that scanf is bad and shouldn't be used if possible and other places that gets is worse than scanf. Which should I be using and if it depends when shoulld I use which?
    ~Sven

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Use neither. fgets() is the best. See the FAQ.

    Why gets() is bad: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Why fgets() is good: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    scanf("%s", s) is bad because it stops at spaces, among other things.
    Last edited by dwks; 02-14-2006 at 02:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    What do I put where the file pointer goes in fgets then?
    ~Sven

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    #include <string.h>  /* for strchr */
    
    char s[SOME_SIZE], *p;
    fgets(s, sizeof(s), stdin);
    /* or sizeof s, or SOME_SIZE */
    /* now you have a line, but it's newline terminated */
    if((p = strchr(s, '\n'))) *p = 0;
    /* now you have what you want */
    printf("Hello, %s!\n", s);
    I often use BUFSIZ for SOMESIZE. It's at least 512 and defined in <stdio.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Read my second FAQ link (you might have caught my post before I edited it). This is the sample program provided:
    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    int main()
    {
      char buf[BUFSIZ];
      char *p;
      
      printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
      
      if (fgets(buf, sizeof(buf), stdin) != NULL)
      {
        printf ("Thank you, you entered >%s<\n", buf);
        
        /*
         *  Now test for, and remove that newline character
         */
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
          
        printf ("And now it's >%s<\n", buf);
      }
      
      return 0;
    }
    
    
    /*
     Program output:
    
    Please enter a line of text, max 512 characters
    this is a test
    Thank you, you entered >this is a test
    <
    And now it's >this is a test<
    */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    You should read
    this

    Prelude is cerainly master of the universe, too bad she's not with us anymore...
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    She isn't?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21