What is the difference between scanf and fgets? - ProProfs Discuss
Advertisement

What is the difference between scanf and fgets?

Asked by R. Barnes, Last updated: Apr 14, 2024

+ Answer
Request
Question menu
Vote up Vote down

4 Answers

N. Reyes

N. Reyes

A good writer and orator as well.

N. Reyes
N. Reyes, Writer, B.ed, California

Answered Oct 22, 2019

Those who may not be too familiar with codes may assume that scanf and fgets are the same, but these are two different things. Fgets can be used to read different open files while the scanf will only be able to read the available standard input. Fgets can also be used to read a line or a text that can be found in a certain file, but the scanf can read more lines or texts.

Those who may not be too familiar with codes may assume that scanf and fgets are the same, but
This will also have the ability to read some built-in numeric types and also some strings. Most people will use Fgets when they want to read the line, but if they want to get different data from the line, they will use a scanf so that they can dissect the line easily.

upvote downvote
Reply 

H. Jones

H. Jones

H. Jones
H. Jones, Web Content Writer, San Antonio,

Answered Sep 11, 2019

Scanf and gets are both C functions that read inputs made by a user from standard input. Although they have some similarities, scanf and gets, have some notable differences. Scanf is mostly used to read single characters from a user. It continues to read inputs until it encounters space or newline. Scanf can read many different data types.

Scanf and gets are both C functions that read inputs made by a user from standard input. Although
Scanf can also guard against overflow; this is possible as it puts the buffer size in the format string. Scanf is also more flexible than gets On the other hand, gets will register a line as a string (a bunch of characters). Gets considers whitespace as a part of the string. Gets is only used for string input. It only ends when the user hits enter or end of file (EOF). I hope you find this information helpful.

upvote downvote
Reply 

J. Pollock

J. Pollock

J. Pollock
J. Pollock, Science Professor, Los Angeles

Answered Sep 04, 2019

Gets is normally used in order to read input from the keyboard. This can also be used to read the data that can be found at the End of File. Scanf will read the input that is placed until it encounters whitespace. There are also instances when it will only end when it reaches the EOF.

Gets is normally used in order to read input from the keyboard. This can also be used to read the
Scanf will also read different data types while Gets will only read one character string data at a time. If you would need to read string input then you know that Gets is the one that you need. If you need to read other data types, Scanf will be the better option. One of the things that people need to train for is to understand how to read complete sentences using Scanf or gets.

upvote downvote
Reply 

Bobby Rickets

Bobby Rickets

Bobby Rickets
Bobby Rickets, Content Reviewer

Answered Sep 03, 2019

Scanf will have the ability to read the standard input, but if you would use fgets, this will be able to read the open file. Fgets will also have the ability to read the lines from a file quickly, which is crucial when this is something that you need. This can also handle the conversion of certain numeric types.

Scanf will have the ability to read the standard input, but if you would use fgets, this will be
Fgets is commonly used to read data then once they have found the lines that they want, they will use scanf to dissect the data further and gather more details. The pattern that will be used for scanf will depend on the person who is using it, which means that it will depend on you.

upvote downvote
Reply 

Advertisement
Advertisement
Search for Google images Google Image Icon
Select a recommended image
Upload from your computer Loader
Image Preview
Search for Google images Google Image Icon
Select a recommended image
Upload from your computer Loader
Image Preview
Search for Google images Google Image Icon
Select a recommended image
Upload from your computer Loader

Email Sent
We have sent an email to your address "" with instructions to reset your password.