Ad space (banner)
๐Ÿ”งC Lessons
Lesson 58 / 68

Reading from Standard Input

This lesson covers receiving standard input in C using fgets, so you can build a program that processes user input. It's for anyone searching "C fgets usage."

fgets() lets you receive one line of input (standard input) from a keyboard or similar source. Use it when you want a command-line tool to prompt the user for something.

The example receives a string from standard input with fgets(name, sizeof(name), stdin), then finds the position of the newline character with strcspn(name, " ") and sets it to 0 to strip the trailing newline.

A common early mistake is not knowing fgets() leaves the newline character at the end of what it reads. Unlike some other languages that automatically strip it, C requires you to write that logic yourself.

In real projects, using an input value without validating it first can lead to unexpected errors, so combining it with a validation step is safer.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)