The difference between fgets and fgetc | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

The difference between fgets and fgetc

What’s the difference between fgets and fgetc in c ? And when should I use each one ?

29th Jun 2022, 11:03 PM
Kawtar
Kawtar - avatar
2 Answers
+ 4
Kawtar fgetc() is used to obtain input from a file single character at a time 'so it's returns an int'. while the fgets() function in C reads up to n characters from the stream (file stream or standard input stream) to a string. 'returns char pointer ' Each time you read (either via fgetc or fgets) you are making a system call which takes time, you want to minimize the number of times that happens, so calling fgets fewer times and iterating in memory is faster
30th Jun 2022, 1:21 AM
Amine Laaboudi
Amine Laaboudi - avatar
+ 2
fgets() will read the whole string upto the size specified in argument list but when end of line occurs fgetc() returns EOF while fgets() returns NULL
30th Jun 2022, 2:50 AM
Vaibhav
Vaibhav - avatar