Skip to main content

Posts

Showing posts with the label Command-line Arguments in C

Command-line Arguments in C

Command-line arguments are a way to pass information or inputs to a C program when you run it from the command line or terminal. These arguments provide additional flexibility and let users control the program's behavior without changing the code. In C, command-line arguments are handled through the  main  function, which takes two parameters: int main(int argc, char *argv[]) Here’s what these parameters mean: argc  (Argument Count): This integer represents the number of command-line arguments, including the program name. argv  (Argument Vector): This array of strings ( char *argv[] ) holds each command-line argument as a string.  argv[0]  is always the program's name, and the remaining elements are the actual arguments passed by the user. Example: Printing Command-Line Arguments Let’s start with a basic example where we simply print all command-line arguments. #include <stdio.h> int main(int argc, char *argv[]) {     printf("Number of arguments: %d\n", argc);