What is the difference between get and scanf?

If the code

scanf("%s\n",message) 

against

 gets(message) 

What's the difference? Both of them seem to be typing a message.

+7
c scanf gets
source share
10 answers

The main difference [from your specific scenario],

  • scanf() completes data entry when meeting with whitespace , newline or EOF

  • gets() treats the space as part of the input string and completes the input when it encounters newline or EOF .

However, to avoid buffer overflow errors and avoid security risks, it is safer to use fgets() .

+16
source share

Inconsciousness: in the following context, I consider it “safe” if this does not lead to problems when used correctly. And “unsafe” if “insecurity” cannot be maneuvered.

 scanf("%s\n",message) 

vs

 gets(message) 

What's the difference?

From a security point of view, there are no differences, both read from Standard Input , and very well message overflows, if the user enters more data, then message provides memory for.

While scanf() allows you to use it safely by specifying the maximum amount of data to be scanned:

 char message[42]; ... scanf("%41s", message); /* Only read in one few then the buffer (messega here) provides as one byte is necessary to store the C-"string" 0-terminator. */ 

With gets() you can specify not the maximum number of characters that should be read, so the latter will not be used !

+6
source share

gets - Reads characters from stdin and saves them as a string.

scanf - reads data from stdin and saves it in accordance with the specified int format in the scanf expression, such as %d , %f , %s , etc.

+4
source share

There are some. The first is that gets () will only receive character string data. Another is that gets () will only get one variable at a time. scanf (), on the other hand, is a much more flexible tool. It can read several elements of different data types.

In the specific example that you have chosen, there is not much difference.

+4
source share

The main difference is that gets is read before EOF or \n , and scanf("%s") is read until some empty space is encountered. scanf also provides more formatting options, but at the same time has lower security like gets .

Another big difference is that scanf is a standard C function, and gets removed from the language because it was both redundant and dangerous: there was no protection against buffer overflows. However, the same security flaw exists with scanf, so neither of these two functions should be used in production code .

You should always use fgets , the C standard itself even recommends this, see C11 K.3.5.4.1

Recommended Practice

6 The fgets function allows you to write programs correctly for safe input of input lines for too long to save the resulting array. In general, this requires callers to have the presence or absence of a newline character in the result array. Consider using fgets (along with any necessary processing based on new-line characters) instead of gets_s.

(emphasis mine)

+4
source share

gets: →

 gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte ('\0'). 

ERROR: →

  Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. 

Ex: →

  The scanf() function reads input from the standard input stream stdin; 

ERRORS

  Some times scanf makes boundary problems when deals with array and string concepts. 
+3
source share

In the case of scanf you need the specified format, unlike get. Thus, in gets you enter charecters, strings, numbers and spaces.

In the case of scanf , you enter the ends as soon as a space occurs.

But then in your example you use '% s', so neither gets() nor scanf() , so that the strings are valid pointers to arrays of sufficient length to hold the characters you send them. Therefore, this can lead to buffer overflows.

Tip: use fgets() , but it all depends on the use case

+2
source share

The notion that scanf does not occupy a space is completely wrong. If you use this part of the code, it will also take up white space:

 #include<stdio.h> int main() { char name[25]; printf("Enter your name :\n"); scanf("%[^\n]s",name); printf("%s",name); return 0; } 

If using a new line will stop accepting input. This means that if you press enter only then it will stop accepting input.

So there is no difference between scanf and functions. This is just a complicated way to implement.

0
source share

gets () is not safe, for example: char str [1]; gets (str) if you enter more than the length, this will end with SIGSEGV. if gets can be used, use malloc as a base variable.

0
source share

scanf() is a much more flexible tool, while gets() only gets one variable at a time.

0
source share

All Articles