C Command Line Arguments
In this page:
int main(int argc, char *argv[]) {
// argc: number of arguments
// argv[0]: program name, argv[1]...: the arguments
return 0;
}
What are Command Line Arguments?
Command line arguments let a user configure a program's behavior at launch time without editing or recompiling the source code, by typing extra words after the program name in the terminal.
This is how tools like compilers and file utilities accept filenames, flags, and options.
Example: What are Command Line Arguments?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
// Print formatted text to the screen
printf("Program received %d arguments", argc);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The argc Parameter
argc holds the total count of arguments the program was launched with, and it always includes the program's own name as the first entry, so a program run with two extra words has argc equal to 3, not 2.
Checking argc before accessing argv is essential to avoid reading past the arguments that were actually supplied.
Example: The argc Parameter
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
// Print formatted text to the screen
printf("%d", argc);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The argv Parameter
argv is an array of C-string pointers holding the actual argument text, where argv[0] is conventionally the program's own invocation name and argv[1] onward are the arguments the user actually typed.
Each entry is a plain null-terminated string, so you use standard string functions to parse or compare them.
Example: The argv Parameter
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
// Print formatted text to the screen
printf("%s", argv[0]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading Argument Values
To use the arguments passed at launch, index into argv starting from position 1 (skipping the program name at index 0), converting each string to a number with atoi() or similar if the argument represents numeric input rather than text.
Process them in the order the user supplied them, since argv preserves that order exactly.
Example: Reading Argument Values
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
char *fakeArgv[2] = {"prog", "42"};
// Declare value and set it to atoi(fakeArgv[1])
int value = atoi(fakeArgv[1]);
// Print formatted text to the screen
printf("%d", value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Validating Argument Counts
Because argv is only as long as argc says it is, reading argv[i] for an i that exceeds argc-1 reads unallocated memory and can crash or behave unpredictably.
Always compare the expected argument count against argc, and print a usage message rather than proceeding, before touching any index beyond what was actually passed.
Example: Validating Argument Counts
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
// Check whether argc < 2
if (argc < 2) {
// Print formatted text to the screen
printf("Not enough arguments");
// Send 1 back to the caller
return 1;
}
// Print formatted text to the screen
printf("%s", argv[1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting that
argv[0]is the program name and the first real argument isargv[1]. - Reading
argv[1]without first checking thatargcis large enough. - Using an argument as a number without converting it, since every
argventry is a string.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions