commandline

C Command-Line Arguments

C Command-Line Arguments

Command-line arguments allow you to pass information to a program when executing it from the terminal.

Using Command-Line Arguments

#include 

int main(int argc, char *argv[]) {
    printf("Number of arguments: %d\n", argc);
    printf("Arguments passed: \n");

    for (int i = 0; i < argc; i++) {
        printf("%s\n", argv[i]);
    }

    return 0;
}
    

This program prints the number of command-line arguments and displays each one passed to the program.

Example: Running ./program arg1 arg2 will output:

Number of arguments: 3
Arguments passed:
./program
arg1
arg2