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

Reading Command-Line Arguments

This lesson covers reading command-line arguments in C, so a program can accept a value from outside at runtime. It's for anyone searching "C command-line arguments usage."

You can receive command-line arguments through the main function's parameters, int main(int argc, char *argv[]). argc is the argument count, and argv is an array of argument strings.

The example checks whether enough arguments were passed with argc > 2, displaying argv[1] as the name and argv[2] as the age. Notice that argv[0] represents the executable's own path.

A common early mistake is accessing an argument without checking the count first, causing an error. You need to check the count with argc beforehand.

In real projects, this is also a useful foundation for a simple automation tool that branches its logic based on a value passed at runtime.

๐Ÿ“– 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)