Thread: Hello getting problem with gets() function

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    Hello getting problem with gets() function

    My problem is that the gets() function takes the whole input rather than staying to the limit. Here is what i do:

    char c[10];
    puts("User enter yoir name not more than 10 characters: ");

    gets(c);

    printf("%s", c);


    But i can give any large input and it takes that

    Input: Dushyant Kaushik
    Output: Dushyant Kaushik

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, this is a well-documented flaw of gets().
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Use of gets even generates a warning under certain compiler settings.

    fgets is considered the safe alternative.
    fgets(c, 10, stdin);

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.
    gets, gets_s - cppreference.com

  4. #4
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    Thanx for help

    Now it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-02-2016, 07:12 AM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM

Tags for this Thread