Why gets method is not shown in autocomplete? - c

I am using Visual Studio 2015 and ReSharper for my C programs but I can not make gets method work in this IDE. Why is this method not shown in autocomplete list?

From the C documentation:.
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().

Related

Can not use gets in visual studio 2019 [duplicate]

I have faced a compiler error(c3861) in my newly installed Visual studio community 2015 IDE:
I just want to use gets() function from stdio.h library, and i have included stdio.h file in my program, but compiler show me a compiler error
like below:
error C3861: 'gets': identifier not found
What should i do to compile my program correctly withgets() function.
Since C11, gets is replaced by gets_s. The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflows. The recommended replacements are gets_s() or fgets()
gets_s(buf);
fgets(buf, sizeof(buf), stdin);
if you are looking forward to learn about
buffer overflow vulnerability
you simply can use it and anther unsafe functions by the fallowing steps
from the solution explorer right click on the project and choose properties
navigate to Configuration Properties >> C/C++ >> Advanced
change Compile As value to Compile as C Code (/TC)
(optional) if you would like to disable the warning just put its warning number in disable specific warning
The gets function was considered too dangerous (because it can easily cause a buffer overflow), so it was removed from the latest revisions of both C and C++.
You are supposed to use fgets instead. With that function you can limit input to the size of your buffer.
gets and_getws are removed from the beginning of vs 2015 because these functions are obsolete.
Alternative functions are gets_s and _getws_s.

Use of getch function and gets and puts functions

Experts I've doubts in gets(),puts() and getch().
Why do we use gets() and puts() when we have scanf() and printf()?
What is the use of getch().
Please explain this in simple language because I'm a beginner.
THank you in advance. :)
gets doesn't exist anymore (except in outdated environnments such as the infamous TurboC), use fgets instead.
fgets reads one line of text from a file including from the terminal (standard output)
puts writes one line of text to the terminal (standard output)
fputs writes one line of text to a file, including the terminal (standard output)
getch reads on character from the standard input (terminal)
printf and friends allow you to print formatted output
scanf and friends allow you to read formatted input.
Why do we use gets() and puts() when we have scanf() and printf()?
We don't use gets(), the function was so poorly designed that it was flagged obsolete 18 years ago and finally completely removed from the C language 6 years ago. If someone taught you to use gets(), you need to find a more updated source of learning. See What are the C functions from the standard library that must / should be avoided?.
puts(str) is only used as a micro-optimization of printf("%s", str). puts() is traditionally ever so slightly faster than printf(), since it doesn't need to parse a format string. Today, this performance benefit is a non-issue.
However, puts() is much safer than printf(). You can write bad code like printf("%s", pointer_to_int) and it might compile without warnings. But puts(pointer_to_int) will never compile without warnings/errors.
Generally, most of stdio.h is dangerous and unsafe because of the poor type safety. It is avoided in professional, production-quality code. For student/hobbyist purposes, printf/scanf are however fine.
What is the use of getch()
getch() was a non-standard extension to C, which allowed programs to read a character from stdin without echoing the typed character to stdout (the screen).
This function was popular back in the days of MS DOS when Borland Turbo C was the dominant compiler. Because Turbo C had it, some other compilers also started supporting it together with the non-standard conio.h library. We're talking early 1990s.
You shouldn't use this function in modern C programming.
(Though it is possible to implement it yourself with various API calls, as in this example for Windows.)

Get_s function not working

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?
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
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.

Why C standards contain many unsafe functions, which are useless?

Why C standards contain many unsafe functions, which are useless (in good programs them don't use) and harmful, for example getchar? Why C standard doesn't contain instead of them the useful functions, for example getch, and getche? It is only one of many examples...
UPD
I confused: gets instead of getchar.
Do you mean gets? To not break old programs. The road to obsoleteness is long.
And besides, it has been deprecated.
gets is deprecated in C99 and has been removed in C11.
C11(ISO/IEC 9899:201x) Forward/6
removed the gets function ()
You can't implement getch() [reading without buffering] on a teletype (terminal that looks like a typewriter). Or any type of terminal where the data is stored in the terminal until you hit enter.
There are functions that do this sort of things, but remember that C is a language that is supposed to "run on anything".
gets was part of the standard library many years ago, so it has to stay - otherwise, old code won't compile, and a lot of people like to use old code (because it's a lot of work to fix up 1000000 lines of messy code that used to work!)

Status of POSIX implementations

I just found out that the getline() function which is originally a GNU extension is now part of the POSIX 2008 standard.
Is there some overview of how broadly is this version of POSIX supported?
I would just go ahead and use it. It's possible to implement getline (but not the more powerful getdelim) as a very fast/efficient wrapper around fgets, so if you're willing to do that as a fallback, using getline doesn't really make your program any less portable. (Versus something like using __fpending, which cannot be implemented portably and requires an implementation-specific hack to emulate.)
Also, of course, both getline and getdelim can simply be implemented on top of flockfile/funlockfile and fgetc. It's not very efficient, but it will work anywhere.

Resources