~/Usage of fputs, fgets, fflush, and scanf in C
Feb 13, 2019
The fputs function writes a string to the specified file stream. The fgets function reads a line from a file or stdin, including the newline. To clear input or output buffers, fflush is used. scanf reads formatted input, usually from stdin.
Below is a concise example:
|
|
Notes:
- Use fgets to read lines safely including spaces and newlines.
- fflush is only guaranteed to flush output streams; its use with stdin is not portable.
- fputs outputs strings, commonly to stdout or to files.
- scanf is suitable for simple, space-delimited input.
For more on safe input, see C input handling.