Appearance
[中文版本](/posts/c教程/zh-CN/05-输入输出与文件/047-Command-Line Arguments (argc, argv))
Every C program can receive input directly from the command line, noscanf, nofgets, just arguments passed when you run the executable. This is how professional C tools (likegcc,ls, andgrep) receive filenames, options, and flags.
The Function Signature
Yourmain function can take two parameters:
int main(int argc, char *argv[])| Parameter | Meaning |
|---|---|
argc | Argument count (number of command-line arguments) |
argv | Argument vector (array of C strings, each argument) |
argv[0] is the program name itself, andargv[1] onward are the user-provided arguments.
Example:
./hello world testThen:
argc == 3argv[0] = "./hello"argv[1] = "world"argv[2] = "test"
Tiny Code: Print Command-Line Arguments
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Argument count: %d\n", argc);
for (int i = 0; i < argc; i++)
printf("argv[%d] = %s\n", i, argv[i]);
return 0;
}Run it:
./args foo bar 123Output:
Argument count: 4
argv[0] = ./args
argv[1] = foo
argv[2] = bar
argv[3] = 123Checking for Missing Arguments
If your program needs arguments, checkargc before accessing them.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
printf("Opening file: %s\n", argv[1]);
return 0;
}Run it:
./fileopOutput:
Usage: ./fileop <filename>Run again:
./fileop data.txtOutput:
Opening file: data.txtConverting String Arguments to Numbers
All command-line arguments are strings. To use them as numbers, convert using:
atoi()– string to intatof()– string to floatstrtol()/strtod()– safer and more flexible alternatives
Example:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
return 1;
}
int a = atoi(argv[1]);
int b = atoi(argv[2]);
printf("%d + %d = %d\n", a, b, a + b);
return 0;
}Run:
./sum 10 25Output:
10 + 25 = 35Handling Options (Flags)
You can build simple command-line tools that handle options manually:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int verbose = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-v") == 0)
verbose = 1;
}
if (verbose)
printf("Verbose mode on\n");
else
printf("Run quietly\n");
return 0;
}Run:
./tool -vOutput:
Verbose mode onTiny Code: Mini File Echo Tool
This program prints the content of a file passed as an argument:
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "r");
if (!fp) {
perror("Error");
return 1;
}
char buf[128];
while (fgets(buf, sizeof(buf), fp))
printf("%s", buf);
fclose(fp);
return 0;
}Run:
./echo mytext.txtWhy It Matters
argc andargv make your C programs scriptable and composable:
- Automate tasks from the command line
- Integrate with shell scripts or pipelines
- Process multiple input files
- Implement command-line flags and options
Every real-world C utility, fromls togcc, depends on this pattern.
Try It Yourself
Write a program that takes a list of integers and prints their sum.
Add a-r flag to reverse the order of printed arguments.
Build a “greet” tool:
./greet Alice Bob Charlie→Hello, Alice! Hello, Bob! Hello, Charlie!
Write a “compare” tool that checks if two file names are identical.
Combineargc and file I/O: copy one file to another with
./copy source.txt dest.txtWith command-line arguments, your C programs evolve from static exercises to flexible, real-world tools. Next, you’ll explore reading configuration files, a powerful way to let your programs adapt automatically without recompilation.
