1
#include <stdio.h>

int main() {
   char str[50];

   printf("Enter a string : ");
   gets(str);

   printf("You entered: %s", str);

   return (0);
}

In my code, why isn't the gets() function declared? It shows me a bunch of errors such as:

In function ‘int main()’:
 error: ‘gets’ was not declared in this scope
gets(str);
    ^~~~
[Finished in 0.2s with exit code 1]

I want to know why this kind of problem occurs?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • You have declared a `gets` function – Jacob Feb 16 '20 at 06:13
  • 4
    Are you asking about C or C++? Please tag only the one that you are actually compiling as. The two languages are different. – walnut Feb 16 '20 at 06:17
  • @Jacob `gets` is a standard C function which *was* declared in `stdio.h`. – walnut Feb 16 '20 at 06:20
  • 1
    See [Why `gets()` is too dangerous to be used — ever!](https://stackoverflow.com/q/1694036/15168). – Jonathan Leffler Feb 16 '20 at 14:44
  • 1
    Since `gets()` is inherently dangerous and is no longer part of Standard C, your compiler and system have decided not to declare `gets()` in ``, and the compiler options you're using require functions to be declared before they're used. It would be interesting to know which version of which compiler you're using, and which system (o/s and version) you're using it on. It is a huge step in the right direction, if you ask me. The chances are high that the function is still defined in the library, so if you specify an old version of the standard (don't!), it compiles. – Jonathan Leffler Feb 16 '20 at 15:02
  • @MD.TahuruzzohaTuhin: you can accept one of the answers by clicking on the grey checkmark below its score – chqrlie Feb 20 '20 at 08:45

4 Answers4

9

gets has not been part of the C language for the past 9 years, and prior to that, was deprecated and extremely unsafe - not just difficult to use in a safe/correct manner, impossible. Whoever taught you C is not fit to be teaching.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    P.S. Most of such teachers are egoistic and students are afraid to make that comment by the fear of losing internal marks – Ardent Coder Feb 16 '20 at 07:08
3

I didn't know gets was deprecated, but you can use fgets. https://en.cppreference.com/w/c/io/fgets

Where you have to specify the maximum size available in the buffer (which protects against buffer overflow).

fgets(buffer, sizeOfBuffer, stdin)

Be aware

That fgets also reads the newline character into the buffer while gets doesn't (as mentioned in the comments). So you have to remove the newline character afterwards if you are not interested in it.

Salizer
  • 71
  • 5
  • 3
    Not just deprecated, _removed from the language_. If I compile a C program using `gets()` with `gcc -std=c99` the compiler finds the function, but gives a warning about deprecation. If I compile the same program using the `-std=c11` option, the compiler does not find the `gets()` function. `fgets()` is the right choice, but you might mention that `fgets()` reads the newline into the input buffer, while the `gets()` function discarded the newline. This means that code using `fgets()` often needs to handle the newline. – ad absurdum Feb 16 '20 at 15:11
  • 2
    `fgets()` is not a drop in replacement for `gets()`: unlike `gets()`, `fgets()` does not strip the trailing newline, – chqrlie Feb 16 '20 at 16:24
2

I am assuming you are wanting to get keyboard input from the user?

If you are using c++ you can use cin >> str

If you are using c you will want scanf("%s", &str)

gets was deprecated in C++11 and removed from C++14

Jacob
  • 887
  • 1
  • 8
  • 17
  • 2
    Both of your options replicate the problem that `gets` had and for which it was removed: If the user enters more characters than `str` is long, there will be an unavoidable buffer overflow. You should recommend a safe option instead. – walnut Feb 16 '20 at 06:25
  • 1
    The code using `cin` grows `str`, does it not? The `scanf()` lone does not need the `&`, though. – Jonathan Leffler Feb 16 '20 at 07:56
  • 3
    @JonathanLeffler Assuming `str` is a `std::string`, then `cin >> str` is fine. But if `str` is the `char str[50];` from the question, then it is not fine. There is a [`operator>>` overload for `char*`](https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2) without size information. Fortunately it will be replaced by a overload taking a reference to `char[N]` in C++20, that can at least not overflow the buffer. – walnut Feb 16 '20 at 14:41
  • 1
    Oh, yes — grump about cross-language questions! – Jonathan Leffler Feb 16 '20 at 14:42
1

gets() has been removed from the C language. This function cannot be used safely because the size of the destination array is not provided, hence a long enough input line will cause undefined behavior as gets() will write beyond the end of the array.

Here is a replacement function that takes an extra argument:

#include <stdio.h>

// read a line from stdin, ignore bytes beyond size-1, strip the newline.
char *safe_gets(char *dest, size_t size) {
    int c;
    size_t i = 0;
    while ((c = getc(stdin)) != EOF && c != '\n') {
        if (i + 1 < size)
            dest[i++] = c;
    }
    if (size > 0)
        dest[i] = '\0';
    return (i == 0 && c == EOF) ? NULL : dest;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189