Question:
distinguish between scanf( ) and gets( ) in programming world?
turef
2009-02-17 02:03:29 UTC
please give me atleast four points, urgent.
Seven answers:
aysh
2009-02-17 02:11:30 UTC
gets - Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached.



scanf- Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments.
jplatt39
2009-02-17 02:27:13 UTC
scanf() uses printf()'s format strings. Thus you have to tell it for example that you want a string, a float, an integer character of some kind or a char. In fact, the first difference is you can enter any of the basic types that way and the second difference is that you use the format string to tell it what you enter. Gets() enters strings period.



Scanf() does format your input. If you enter a whitespace character like a space or -- anything else -- your string ends whether you want it to or not. Gets() does not. You type until you reach a newline character (hit enter). When it encounters that, that is your string.



One similarity is kinda dangerous, and leads to the next difference I can think of. Neither checks, when using strings, that the strings are valid pointers to arrays of sufficient length to hold the characters you are sending to them. C tends to treat arrays and pointers the same way, so this behavior can cause crashes or worse if you send your data to or try to read it from uninitialized pointers.



Gcc likes scanf(). If you compile a program with gets you get the following in GCC:

/tmp/ccmTbwvW.o: In function `main':

get.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used.

You don't get that "problem" with scanf.



Finally you can do a:

HelloString=gets(Hellostring);



Gets() returns another pointer to your array of char which you can throw away if you like. Scanf() also has a return. It returns the number of items successfully read or EOF. You can use it to check that you got everything or throw it away if you like.
Arushi B
2009-02-17 02:47:03 UTC
scanf is a function that reads data with specified format from a given string stream source, originated from C programming language, and is present in many other programming languages.

The scanf function prototype is:

int scanf (char *format, ...);

The function returns the total number of items successfully matched, which can be less than the number requested. If the input stream is exhausted or reading from it otherwise fails before any items are matched, EOF is returned.

So far as is traceable, "scanf" stands for "scan format", because it scans the input for valid tokens and parses them according to a specified format.

http://en.wikipedia.org/wiki/Scanf

ets is a function in the C standard library, declared in the header file stdio.h, that reads a line from the standard input and stores it in a buffer provided by the caller.

Use of gets is strongly discouraged. It is left in the C89 and C99 standards for backward compatibility. Many development tools such as GNU ld emit warnings when code using gets is linked.

The programmer must know a maximum limit for the number of characters gets will read so he can ensure the buffer is big enough. This is impossible without knowledge of the data. This design flaw leads to bugs and opens a gate for exploiting computer security through a buffer overflow. Many sources advise programmers to never use gets in new programs.[1][2][3]

http://en.wikipedia.org/wiki/Gets
rhoat
2016-11-11 02:13:56 UTC
Gets C Programming
?
2016-10-25 07:47:28 UTC
Nope. the following is the precedence: even as an account is lifted, this is mere uncooked numbers and an research is going no deeper. trolls, who do no longer care about the expenditures besides, have yet another on the waiting even as that one is bumped off sturdy customers move by ability of the charm procedure, in straightforward words to earnings they are undesirable and the account is honestly no longer restored
LRR
2009-02-17 02:15:34 UTC
scanf will read the data

gets ???
Goj
2009-02-17 02:13:00 UTC
It is use for same order...to input values


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...