I want to know what is the difference between fgets()
and scanf()
. I am using C as my platform.
sscanf() reads formatted input from a string. scanf() reads formatted input from stdin . So in your example sscanf reads from the first argument of the program and saves it in the variable port .
Even though both the functions, gets() and fgets() can be used for reading string inputs. The biggest difference between the two is the fact that the latter allows the user to specify the buffer size. Hence it is highly recommended over the gets() function.
scanf is a C function to read input from the standard input until encountering whitespace, newline or EOF while getchar is a C function to read a character only from the standard input stream(stdin), which is the keyboard. Thus, this is the main difference between scanf and getchar.
There are multiple differences. Two crucial ones are:
fgets()
can read from any open file, but scanf()
only reads standard input.fgets()
reads 'a line of text' from a file; scanf()
can be used for that but also handles conversions from string to built in numeric types.Many people will use fgets()
to read a line of data and then use sscanf()
to dissect it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With