What is the difference between scanf and fgets? - ProProfs Discuss
Topics-
Products +
     

What is the difference between scanf and fgets?

Asked by R. Barnes, Last updated: Sep 11, 2019

+ AnswerRequest Answer
...

4 Answers

N. Reyes

A good writer and orator as well.

N. ReyesWriter, B.ed, California
Writer, B.ed, California

Answered on 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.

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.

 Reply

H. Jones

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

Answered on 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 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.

 Reply

J. Pollock

J. PollockScience Professor, Los Angeles
Science Professor, Los Angeles

Answered on 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.

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.

 Reply

B. Rickets

B. Rickets

Answered on 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.

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.

 Reply

Search for Google images
Select a recommended image
Upload from your computer
Search for Google images
Select a recommended image
Upload from your computer
Search for Google images
Select a recommended image
Upload from your computer

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