Questions tagged [gets]

Anything related to C or C++ standard library functions gets (C) or std::gets (C++). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string. DO NOT USE THESE FUNCTIONS: they are deprecated, and since C11 gets is no longer part of the standard.

Anything related to C or C++ standard library functions gets (defined in <stdio.h> C standard header) or std::gets (defined in <cstdio> C++ standard header). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string. There are no constraints on the functions to prevent them writing outside the bounds of the array that is passed.

DO NOT USE THESE FUNCTIONS: they are dangerous and deprecated for security reasons because they are susceptible for easily causing buffer overflow. For more information about why the use of gets is harmful, consider the link below.

Since C11 (ISO/IEC 9899:2011) gets has been removed from the C standard library. Annex K of the C standard defines an optional replacement function called gets_s for backwards-compatibility reasons, but makes the following recommendation to use fgets whenever possible:

ISO 9899:2011 K.3.5.4.1

Recommended practice

The fgets function allows properly-written programs to safely process input lines too long to store in the result array. In general this requires that callers of fgets pay attention to the presence or absence of a new-line character in the result array. Consider using fgets (along with any needed processing based on new-line characters) instead of gets_s.

Note that gets_s() is not generally available except on Windows using the Microsoft C library.

See CPPreference.com:

422 questions
5
votes
7 answers

Why is gets() not consuming a full line of input?

I'm trying to use gets() to get a string from the user, but the program seems to be passing right over gets(). There is no pause for the user to give input. Why is gets() not doing anything? char name[13]; printf("Profile name:…
Ace
  • 462
  • 4
  • 10
  • 17
4
votes
3 answers

gets() does not read user input

I am new to linked list, now I have little problems in population of nodes. Here I could populate first node of linked list but the gets() function doesn't seems to pause the execution to fill the next node. Output is like: Var name : var Do you…
Bijoy
  • 399
  • 2
  • 7
  • 15
4
votes
3 answers

Is there any gcc compiler option or something similar to bypass the obsolesence of the gets function?

Let's get this out of the way first: I am fully aware of why the gets function is deprecated and how it can be used maliciously in uncontrolled environments. I am positive the function can be used safely in my setup. "Alternative" functions won't do…
Nu Nunu
  • 376
  • 3
  • 14
4
votes
3 answers

Why `gets_s()` still isn't implemented in GCC (9.3.0)?

I know fgets() is a more common and widespread option for string input, but C11 has been around for 9 years. Why is gets_s() still out of work? Even when I add -std=c11, it still doesn't work, even though gets_s() should be in stdio.h.
brushmonk
  • 109
  • 7
4
votes
2 answers

fgets() / gets() problem while taking input for N strings. Not taking input at initial position

I need to get input for n (user-inputted) strings. For that I start with defining a two-dimensional array char str[ ][ ]. I used for loop getting input from user and have tried gets(), fgets() both. In code example although I used gets(). But it is…
Vartika
  • 77
  • 1
  • 6
4
votes
2 answers

How to capture some special key presses for Ruby input prompt in Terminal (and let others through normally)

I am in a Ruby script something like: something = gets # or STDIN.gets puts something def arrow_pressed puts "arrow totally pressed" end Once the user prompt exists I want all keyboard input to happen normally except for the arrow keys.…
PlacateTheCattin
  • 1,356
  • 2
  • 11
  • 10
4
votes
3 answers

Why is implicit declaration of gets() not allowed in C99?

I am starting to learn programming in C language the book I am refering to code shows some source code with gets() and my IDLE recognises it as well. But still while compiling it, my compiler doesn't agree with it. Can anyone help me out? I am using…
Mr. Anderson
  • 49
  • 1
  • 1
  • 3
4
votes
2 answers

Alternative to gets?

I used to use gets but then I heard that it got removed from c11 and that its overall very dangerous. So I did some searching and found out that you can use fgets() to do the same thing. The problems is that when I do use fgets() it seems to also…
Mathue24
  • 152
  • 1
  • 2
  • 8
4
votes
2 answers

Stack around the variable was corrupted

I am having a problem with gets. The purpose is to get input from the user until he hits the 'Enter'. This is the code: struct LinkedListNode* executeSection2() { char inputArr [3] = {'\0'}; struct LinkedListNode* newNode; struct …
user1673206
  • 1,671
  • 1
  • 23
  • 43
3
votes
1 answer

Changing behavior of gets, when reading from command line, in Ruby

The intended operation of the Ruby code below is as follows: write ARGV[0], a file named on the command line, to old create a new, temporary copy of that file loop until user gives input remove the temporary file When I hard code old equal to…
user992236
  • 33
  • 2
3
votes
6 answers

Reading strings in C

If I was using C gets(), and I was reading a string from the user, but I have no idea how big of a buffer I need, and the input could be very large. Is there a way I can determine how large the string the user inputted was, then allocate memory and…
Josh
  • 6,046
  • 11
  • 52
  • 83
3
votes
1 answer

How to disable the warning about using deprecated gets in GCC?

I'm running a CTF and I am currently writing a problem that exploits C's gets function. I understand that the function is deprecated and dangerous and I would never use it in any other circumstance. Unfortunately, gcc compiles my code and when I run…
will
  • 137
  • 3
  • 9
3
votes
3 answers

Why does gets(stdin) return an integer? And other errors

I am new to C programming (although I have experience with Java). After reading some tutorials, I decided to start solving coding challenges on Coderbyte. The first challenge I tried was this one: Challenge Have the function FirstFactorial(num)…
Limon
  • 63
  • 8
3
votes
2 answers

c - how gets() work after scanf?

I have two questions: why only when i do space in "%d " --> scanf("%d ", &num); it works? I tried fflush(stdin) \ _flushall() between the scnaf and the gets and it doesn't works, it skips the gets. When I do the space, it first does scanf then…
asaf
  • 119
  • 2
  • 10
3
votes
2 answers

C - gets not working

I'm having a problem with gets() working. The thing is it should be working with just a warning from vars I am not using but the first gets just skips to the second. int addClube(char *fileName) { char qClube2[100], qClubeSimple[100],…
Rafael Botas
  • 125
  • 1
  • 1
  • 9
1 2
3
28 29