Signup/Sign In

Command Line Argument in C

So far we have seen how to input values in C code during compile-time and runtime. To do that, we declared variables in the main() and then worked on them but there is a way to input values without declaring it in the main().

C offers us a feature called "command line argument" using which we can enter values from the command line at the time of execution. Command line argument is a parameter supplied to the program when it is invoked or run.

Use of Command Line arguments in C

  • They are used when we need to control our program from outside instead of hard-coding it.
  • They make installation of programs easier.

Command line argument is an important concept in C programming. Command line arguments are passed to the main() method.

Syntax:

int main(int argc, char *argv[])

Here, argc counts the number of arguments on the command line and argv[ ] is a pointer array which holds pointers of type char which points to the arguments passed to the program.

Let's see a simple code example to check whether any command line arguments is provided to the code or not.

#include<stdio.h>
int main(int argc, char *argv[])
{
    if(argc < 2)
        printf("No argument supplied. The only argument here is %s", argv[0]);

    return 0;
}

Compile the above code using: gcc filename.c -o filename

Then run it using: ./filename

Or you can just use our Online C compiler, but you need to Login to try it out.

Run Code →


No argument supplied. The only argument here is ./a.out

From the above example, we can infer that the first command line argument is the program file name, which is always added by default by the compiler.

Hence, argv[0] = name of our file and argc = 1.

We compiled the above code using command, gcc filename.c -o filename. We provided the -o flag so that the output file is given the name that we provided.)


Example for Command Line Argument

If we want to print all the arguments in our program, we can do it as follows.

#include <stdio.h>
#include <conio.h>

int main(int argc, char *argv[])
{
    int i;
    if( argc >= 2 )
    {
        printf("The arguments supplied are:\n");
        for(i = 1; i < argc; i++)
        {
            printf("%s\t", argv[i]);
        }
    }
    else
    {
        printf("argument list is empty.\n");
    }
    return 0;
}

Compile the above code using command: gcc name_of_file.c, then run it using: ./a.out Welcome to Studytonight, we have provided command line argument while running the compiled code.


1 : Welcome
2 : to
3 : Studytonight

We provided 3 words seperated by space as arguments, while running the code. So, they will be considered as three separate arguments.

Remember that argv[0] holds the name of the program and argv[1] points to the first command line argument and argv[argc-1] gives the last argument. If no argument is supplied, argc will be 1.

Some more examples...

Let's see how inclusion of quotes changes the output of the same program.

Compile the above code again: gcc name_of_file.c

Run it using: ./a.out "welcome to studytonight"


1 : welcome to studytonight

Here, we have provided all the 3 words enclosed in a double quote. So, it is considered as a single argument. Same thing happens with single quotes.

Compile the same code again: gcc name_of_file.c

Then, run it using: ./a.out 'welcome to studytonight'


1 : welcome to studytonight

Similarly, we can give int and float type arguments but they will be treated as strings.

Again compile the above code: gcc name_of_file.c

And run it using: ./a.out 1 0 a 5.07


1 : 1
2 : 0
3 : a
4 : 5.07

Conclusion

In this way, we can use command line arguments in C. Command Line arguments in C is a great way to provide input to the program at runtime making the program resuable with different input values.