0

My book says the get_s() function is a better alternative to the gets() function, which is deprecated and should not be used. But, when I try to use the get_s() function it always gives me an error:

undefined reference to gets_s

This page says something about the gets_s, function that I didn't really understand about it being defined in the ISO/IEC 99. Shouldn' t it work with all the compilers? I'm pretty sure I'm using a very recent version of the MinGW compiler.

How should I use this function? Is using the gets() or scanf() (instead of scanf_s()), or fgets() over fgets_s(), not good?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
BumbleBee
  • 87
  • 2
  • 16
  • 2
    "Shouldn' t it work with all the compilers". No, it is not a standard C function. It is Microsoft specific so basically need Visual Studio. – kaylum Feb 20 '17 at 05:22
  • @kaylum, Is the "ANSI C" different from "microsoft specific" standard? – BumbleBee Feb 20 '17 at 05:40
  • @kaylum, Is it ok to not learn about this standards? How do I get more information about the typical number of standards available? – BumbleBee Feb 20 '17 at 05:41
  • 1
    `gets_s()` is defined in optional Annex K of the C11 (ISO/IEC 9899:2011) standard. It was previously defined in [TR 27431-1](http://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions). It was implemented by Microsoft and offered as a standard, but the Unix (POSIX) community wasn't impressed. C11 unstandardized the `gets()` function — it is no longer part of standard C, though all implementations probably provide it for reasons of 'backwards compatibility'. It is [never safe to use `gets()`](http://stackoverflow.com/questions/1694036/). If available, `gets_s()` is OK. – Jonathan Leffler Feb 20 '17 at 05:45
  • @JonathanLeffler, from above comments, I found there were other standards of C. Do we have to learn all of these standards? – BumbleBee Feb 20 '17 at 05:46
  • 1
    You have to know the environments on which you work — yes. They're all different. Knowing the core C standard helps a lot, but you invariably end up using non-standard functions too. It helps if you know when you're using 'non-standard' functions. Annex K of C11, being optional, is not 'core C standard'. Until recently, a lot of MS C support was firmly C90, not even C99. That has improved, but it isn't there yet. MS has its own island of standards. Unix (including Linux) has POSIX to provide a lot of commonality, but there are differences outside that standard. Yes, you learn a lot of them. – Jonathan Leffler Feb 20 '17 at 05:50

2 Answers2

2

yes you are right #bumblebee 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. Never use gets().

source: http://en.cppreference.com/w/c/io/gets check weather you included the corresponding header. and one more thing u have to see is that weather you c comiler version is an updated version or the old version that can also create a problem.. so try in a c11 standard ,or a c11 online compiler

venky513
  • 321
  • 2
  • 15
  • above in the comments it is stated that the function is "microsoft specific", how do I find out the typical number of standards available? – BumbleBee Feb 20 '17 at 05:43
  • and do we have to learn all of these standards to be a good C programmer? – BumbleBee Feb 20 '17 at 05:44
  • Is MinGW not a C11 compiler? – BumbleBee Feb 20 '17 at 05:44
  • 2
    @BumbleBee: MinGW is not a compiler; it is an environment. The C compiler provided with MinGW is GCC. GCC is a C11 compiler, but the MinGW environment is mostly based on Microsoft's libraries. Since `gets_s()` is provided by MS, it is surprising that you didn't link successfully. I'm not sure I can help with why the linking failed. – Jonathan Leffler Feb 20 '17 at 05:47
  • c++ is a huge programming language ,but being updated with the latest is a good thing , c++ is flexible and can handle many complexities. Thats why the big 4 {google,facebook,apple etc } still rely mostly on it . to get a expert level standard takes years. anyways keep going . all the best .i too started practising recently . please accept my answer if it gave an solution to ur question – venky513 Feb 20 '17 at 05:48
  • 1
    i suggest you to just try the same program on a online c11 standard compiler and check if its working – venky513 Feb 20 '17 at 05:49
  • @JonathanLeffler, " implicit declaration of function 'gets_s' [-Wimplicit-function-declaration]|", it gives me this warning and errors undefined reference to gets_s() – BumbleBee Feb 20 '17 at 05:54
  • @JonathanLeffler, does the GCC also support the elements defined by C11 as optional? – BumbleBee Feb 20 '17 at 05:55
  • 1
    No idea; I've never used MinGW and don't know what version of GCC it has, etc. I know in theory what MinGW is. – Jonathan Leffler Feb 20 '17 at 05:57
  • @venky513, do you know if the GCC supports the elements defined by C11 as optional? – BumbleBee Feb 20 '17 at 05:59
  • which gcc version are you using – venky513 Feb 20 '17 at 06:03
  • 1
    i think this might help you http://stackoverflow.com/questions/16136142/c11-functionality-with-mingw – venky513 Feb 20 '17 at 06:03
  • @JonathanLeffler, is there a page summarizing these optional elements? – BumbleBee Feb 20 '17 at 06:11
  • 1
    @BumbleBee: I don't know of such a page, no. There are too many optional bits in too many systems for such a page to be easy to create or maintain. – Jonathan Leffler Feb 20 '17 at 06:19
  • @venky513, I've installed the latest version – BumbleBee Feb 20 '17 at 06:30
  • I think it could be 6.3(code blocks doesn't show it) – BumbleBee Feb 20 '17 at 06:31
  • In order to use `gets_s` you must check if `__STDC_LIB_EXT1__` is defined by the compiler. If not, the function is not supported. Otherwise if defined, it is part of stdio.h – Lundin Feb 20 '17 at 15:53
0

During early 90s or so, gets() was found to be flawed by design since it would keep reading data forever until it found the end of a string, which meant it could cause buffer overflows either accidentally or through security exploits.

Therefore gets was flagged as an obsolescent function in the C99 standard. Meaning that from the year 1999, people were warned that it should not be used.

The function was removed entirely from the language in the C11 standard, meaning that there was a very generous transit period of no less than 12 years to fix legacy code. It was replaced by gets_s, as a safe alternative to be used when porting old code to C11. It takes the buffer size as second parameter.

However, gets_s should only be used for such C11 porting reasons, if at all. gets_s is part of the optional bounds-checking interface in C11 and compilers need not implement it. The C11 standard recommends to use fgets instead:

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 has little to do with the non-standard Visual Studio compiler, even though that compiler happens to support this function, just as the standard conforming compilers that support the bounds-checking interface (__STDC_LIB_EXT1__) do.

Lundin
  • 195,001
  • 40
  • 254
  • 396