Command Line Arguments
Definition:
Command line arguments allow you to pass values to your main() function when running the program from a terminal.
Syntax:
int main(int argc, char *argv[])
- argc – Argument count (number of arguments)
- argv[] – Argument vector (array of strings)
Example:
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; i++) {
printf(“Argument %d: %s\n”, i, argv[i]);
}
return 0;
}
Run via terminal: ./program Hello World
Output:
Argument 0: ./program
Argument 1: Hello
Argument 2: World