C Notes
Learn how to use command line arguments in C programming with argc, argv, parsing techniques, option handling, and real-world examples for building CLI tools.
Command line arguments allow users to pass information to your program when they run it. Instead of hardcoding values or asking for input during execution, you can make your program configurable right from the terminal. This is how most Unix utilities work – think gcc -o output file.c or ls -la /home.
Understanding argc and argv
Every C program's main() function can accept two parameters that give access to command line arguments:
int main(int argc, char *argv[])argc(argument count) – the number of arguments passed, including the program name itselfargv(argument vector) – an array of strings (character pointers) containing the actual arguments
If you compile this as program and run ./program hello world 42:
Number of arguments: 4 argv[0] = "./program" argv[1] = "hello" argv[2] = "world" argv[3] = "42"
Key points:
argv[0]is always the program name (or path)argv[argc]is alwaysNULL(guaranteed by the C standard)- All arguments are passed as strings – you must convert numbers yourself
Converting Arguments to Numbers
Since all arguments arrive as strings, you need to convert them for numeric operations:
Running ./calculator 3.5 2.5:
Sum: 6.00 Product: 8.75
For safer conversion, use strtol() and strtod() which provide error detection:
Building a Simple File Copy Program
Here's a practical example that mimics a basic cp command:
Parsing Options Manually
Many programs accept flags like -v for verbose or -o filename for output. Here's how to parse them manually:
Running ./program -v -n 5 -o output.txt:
Verbose mode enabled Count: 5 File: output.txt
Using getopt() for Professional Option Parsing
The getopt() function from <unistd.h> provides a standard way to parse command-line options on POSIX systems:
Running ./tool -v -n 10 -o result.txt data.csv:
Input: data.csv Verbose: yes Count: 10 Output: result.txt
Environment Variables
Besides command-line arguments, you can access environment variables using a third parameter or getenv():
argc/argv Properties Table
| Property | Value |
|---|---|
argc minimum | 1 (program name) |
argv[0] | Program name or path |
argv[argc] | Always NULL |
| Argument type | Always char* (strings) |
| Spaces in args | Use quotes: "hello world" |
| Max arguments | System-dependent (usually ARG_MAX) |
Interview Questions on Command Line Arguments
Q1: What is argv[argc] equal to? It's always NULL. The C standard guarantees that argv[argc] == NULL.
Q2: What happens if main() is declared without parameters? The program runs fine but has no access to command-line arguments. The arguments are still passed by the OS but simply ignored.
Q3: Can you modify argv strings? The behavior is implementation-defined. On most systems, you can modify the content of argv strings, but this is generally discouraged.
**Q4: What's the difference between char *argv[] and char argv? They are equivalent in a function parameter context. Both represent a pointer to the first element of an array of char pointers.
Q5: How do you handle filenames with spaces in command-line arguments? The shell handles this – users wrap arguments in quotes: ./program "my file.txt". Your program receives it as a single argv entry.
Summary
Command line arguments through argc and argv are fundamental to building useful C programs. They allow flexible configuration without recompilation, enable scripting and automation, and follow established Unix conventions. For simple programs, manual parsing works fine. For complex tools with many options, use getopt() or getopt_long() to handle options professionally. Always validate argument counts, provide usage messages, and convert string arguments safely using strtol()/strtod() rather than atoi()/atof().
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Command Line Arguments in C – argc and argv Explained.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, advanced, command, line, arguments, command line arguments in c – argc and argv explained
Related C Programming Topics