Thread: fgets vs gets

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    fgets vs gets

    Hi i am new to the topic about strings. Can i know what is the differences between fgets or gets? i cant seem to get the same output.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
      char s1[80], s2[80];
    
    
      fgets(s1, 80, stdin);
      fgets(s2, 80, stdin);
    
    
      strcat(s1, s2);
      printf(s1);
    
    
      return 0;
    }
    and
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
      char s1[80], s2[80];
    
    
      gets(s1);
      gets(s2);
    
    
      strcat(s1, s2);
      printf(s1);
    
    
      return 0;
    }
    Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The string content should be the same... the difference is that fgets() includes a feature to limit input size preventing buffer overflows and gets does not...

    Now you have one more problem... if you are going to strcat() your two strings, you potentially need a lot more than 80 characters... s1 should be declared for *at least* 160... the maximum length *after* joining the strings.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    China
    Posts
    9
    gets(s1) is unsafe when user input more than 80 chars

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wonderwall
    Can i know what is the differences between fgets or gets?
    Other than what has already been mentioned about it being possible to use fgets safely whereas gets is inherently unsafe, one difference is fgets writes the newline character read into the buffer, whereas gets does not. This could account for the difference in output that you observed. Therefore, if this newline character is unwanted, you should search for it (e.g., with strchr) and replace it with a null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf(s1);
    Also, NEVER pass an unknown string as the format string to printf.
    All your malicious user has to do is type in a few % chars and the fun will begin.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by thlgood View Post
    gets(s1) is unsafe when user input more than 80 chars
    gets(s1) is unsafe when user input more than 79 chars

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gets vs fgets
    By nimitzhunter in forum C Programming
    Replies: 2
    Last Post: 08-10-2010, 04:15 PM
  2. fgets help
    By mapunk in forum C Programming
    Replies: 4
    Last Post: 11-30-2005, 07:41 AM
  3. Using fgets()
    By Victor4015 in forum C Programming
    Replies: 4
    Last Post: 11-16-2005, 04:30 PM
  4. fgets
    By Inept Pig in forum C Programming
    Replies: 15
    Last Post: 09-12-2002, 08:27 AM
  5. xor (was fgets)
    By Brian in forum C Programming
    Replies: 3
    Last Post: 08-18-2002, 08:13 PM