Skip to content

C Command Line Arguments

Command line arguments are values passed to a C program when it is executed from the terminal or command prompt. They allow users or scripts to control program behavior or provide input without modifying the source code.

For example, a file copying program might take the source and destination filenames as arguments:

$ copy_program source.txt destination.txt

Here, copy_program, source.txt, and destination.txt are command line arguments.

To receive command line arguments, the main function must be declared with two parameters:

int main(int argc, char *argv[]) { /* ... */ }

Or equivalently:

int main(int argc, char **argv) { /* ... */ }

Parameter Explanations:

  • int argc (Argument Count): An integer representing the total number of arguments passed to the program, including the program name itself.
  • char *argv[] (Argument Vector): An array of character pointers (an array of strings). Each element argv[i] points to a null-terminated string representing one argument.
    • argv[0] always points to the name used to invoke the program.
    • argv[1] points to the first actual argument.
    • argv[2] points to the second actual argument, and so on.
    • argv[argc] is guaranteed by the standard to be a null pointer (NULL).

This program prints all command line arguments it receives.

#include <stdio.h>
// Use the standard signature for main to receive arguments
int main(int argc, char *argv[]) {
printf("Program name (argv[0]): %s\n", argv[0]);
printf("Number of arguments (argc): %d\n", argc);
if (argc > 1) {
printf("Arguments passed:\n");
// Loop through the arguments starting from index 1
for (int i = 1; i < argc; i++) {
printf(" argv[%d]: %s\n", i, argv[i]);
}
} else {
printf("No additional arguments were supplied.\n");
}
// Verify argv[argc] is NULL
if (argv[argc] == NULL) {
printf("argv[argc] is NULL (as expected).\n");
}
return 0;
}

Compile and Run:

$ gcc command_args.c -o cmdargs
$ ./cmdargs first_arg "Second Argument" 123

Expected Output:

Program name (argv[0]): ./cmdargs
Number of arguments (argc): 4
Arguments passed:
argv[1]: first_arg
argv[2]: Second Argument
argv[3]: 123
argv[argc] is NULL (as expected).

Note how arguments containing spaces need to be enclosed in quotes to be treated as a single argument by the shell.

Command line arguments are always passed as strings (char*). If you need to use them as numbers, you must convert them using functions from <stdlib.h>:

  • int atoi(const char *str): Converts string to integer. Limited error checking. Returns 0 on error or if the string isn’t a valid number.
  • long atol(const char *str): Converts string to long.
  • double atof(const char *str): Converts string to double.
  • long strtol(const char *str, char **endptr, int base): (Recommended) Converts string to long integer with robust error checking. endptr can point to the first character not converted. base specifies the number base (e.g., 10 for decimal, 16 for hex).
  • double strtod(const char *str, char **endptr): (Recommended) Converts string to double with robust error checking.

Example using strtol:

#include <stdio.h>
#include <stdlib.h> // For strtol, exit
#include <errno.h> // For errno
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <integer>\n", argv[0]);
return 1;
}
char *endptr; // To check for conversion errors
long number;
errno = 0; // Reset errno before call
number = strtol(argv[1], &endptr, 10); // Base 10 conversion
// Check for conversion errors
if (errno != 0) {
perror("Conversion error");
return 1;
}
if (endptr == argv[1]) {
fprintf(stderr, "Error: No digits were found\n");
return 1;
}
if (*endptr != '\0') {
fprintf(stderr, "Error: Trailing characters found: %s\n", endptr);
return 1;
}
// Conversion successful
printf("You entered the number: %ld\n", number);
printf("Double the number is: %ld\n", number * 2);
return 0;
}

Command line arguments provide a standard way for programs to interact with their execution environment.