Loading...
Loading...
00:00:00

What Is Command Line Arguments?

C programming language is very popular language for creating console based application that is most widely used. A console based application has no GUI(Graphical User Interface) unlike windows 10 or Android 11 Modern operating system. It has only black and white windows called terminal in linux or in windows called CMD(Command Prompt).

When you run your C program using terminal or CMD you can pass some parameters to the main function. These parameters can be used for any purpose just like function arguments. For passing the parameters to main function here is the syntax.

int main(int argc, const char **argv)

We cannot pass parameters to the main function when we run c program using double click method or GUI method.

Examples Of Command Line Arguments

In this example, we are running c program using CMD and passing some arguments. First argument is filename of c program with path and rest of the arguments may be others.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    // argc is number of parameters
    // *argv[] is pointers array(string type)
    for (int i = 0; i < argc; i++) // print all arguments
        printf("The %d argument is %s\n", i, argv[i]);
    printf("Hello World\n");
    return 0;
}
Command Line Arguments in C language
Command Line Arguments in C language