logo of SA Coder
        Menu
sign in

      References



Command Line



If you are familiar with C programming, you might have come across the term "command line". In simple terms, the command line refers to the arguments that are passed to the program when it is run from the terminal. These arguments can be used to change the behavior of the program or provide input data to the program.



In C programming, the command line arguments are passed to the main function through two parameters: argc and argv. The argc parameter stores the number of command-line arguments passed to the program, while the argv parameter is a pointer to an array of strings, where each string represents one argument.



Here is the syntax for the main function with command line arguments:


copy

int main(int argc, char *argv[])
{
    // C code goes here
    return 0;
}



Let's take an example to understand how the command line works in C programming.



Suppose we have a C program called "temp.c" that prints all the command line arguments passed to it. Here is the code for the "temp.c" program:


copy

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("The number of command is %d\n", argc);

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

    return 0;
}



Now, let's compile this program and run it from the command line. Here is how we can do it:


copy

C:user> gcc temp.c -o temp
C:user> ./temp arg1 arg2 arg3



In the above command, we have compiled the "temp.c" program and created an executable file called "temp". Then, we have run this program with three command-line arguments "arg1", "arg2", and "arg3".



The output of the program will be:


copy

The number of command is 4
argument(1) = ./temp
argument(2) = arg1
argument(3) = arg2
argument(4) = arg3



As you can see, the program has printed the number of command-line arguments passed to it and all the arguments one by one.



Now, let's take another example to understand how we can use the command line arguments in our C programs. Suppose we have another C program called "main.c" that calls the "temp" program with some arguments. Here is the code for the "main.c" program:


copy

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

int main()
{
    int ret;
    ret = system("./temp arg1 arg2 arg3");
    printf("Return value of temp is %d\n", ret);
    return 0;
}



In the above program, we have used the "system" function to call the "temp" program with three arguments "arg1", "arg2", and "arg3". The "system" function executes the command passed to it as a parameter and returns the exit status of the executed command.



When we run the "main.c" program, it will call the "temp" program with the given arguments and print the return value of the "temp" program. The output of the program will be:


copy

The number of command is 4
argument(1) = ./temp
argument(2) = arg1
argument(3) = arg2
argument(4) = arg3
Return value of temp is 0





Please login first to comment.