Command Line Arguments in C | FACE Prep

In this article, we will be discussing about Command Line Arguments in detail. Command-line arguments examples and programs with detailed explanation are given below.

What are Command Line Arguments in C?

A lot of us assume that Command Line Arguments is a separate language used to code, but actually, it is just a different method of passing the arguments or values to a program by command line. These command line arguments are useful when you need to control your program from outside instead of hard coding (embedding data directly into the program) the values inside the program.

Command Line Arguments Syntax in C

As you all know, the execution of a program starts from the main() function. So far you would have come across the main function which is of integer(int) return type and doesn’t have any arguments

But, command-line arguments are generally passed to the main function of the program. When you pass these command line arguments, the main function looks as shown below

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

where,

  • argc denotes the number of arguments passed
  • argv is a character pointer array which points to the arguments passed

How to Run these Command line arguments in C?

You can run/execute these command-line arguments in your system in 2 ways

1) Choose a online compiler that lets you enter the command line arguments. One such compiler is – https://www.onlinegdb.com/

2) Write your program in notepad and save it with the extension .c and now, you can open your command prompt window and then enter the input in the below format. Here, 

gcc program_name.c     //program_name is the name with which you saved the program on notepad
a inputvalue    //a is a mandatory command and after this you can enter your input value.

how to run command line arguments in c on your system

Note: For the step 2 to work, you need to have a C compiler already in your system. If you don’t have one then, download it here – https://sourceforge.net/projects/tdm-gcc/ and once you download it, go to

Control Panel —> System —> Advanced System Settings —> Environment variables (Just click on it and a new screen will be displayed which is being divided into two parts) —> In User variables —> Click on new —> write variable name as TDM_home or anything you want and variable value should be the path location (Go to the directory where you installed the compiler and look for bin folder and copy the path, which looks like this – C:\TDM-GCC-64\bin).

Command Line Arguments Examples in C

Here are some examples using Command Line Arguments that will help you understand the functioning of these arguments.

Example #1

Consider the below shown example program with input value as 10.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
    int num1 = atoi(argv[1]);
    printf("Number of arguments: %d\n", argc);
    printf("The entered number is %d", num1);
}

Output:

command line arguments in c

Explanation: The input value 10 is stored in argv. Since argv is a character pointer array, all the input values are stored in the form of ASCII values. So, to convert them to integer we have used atoi function. You need to include #include header file when you use atoi function.

You might wonder why we have started with argv[1]?

In command-line arguments, argv[0] always holds the program name. The program name is also considered as an argument. That’s why argc output is 2 (one is program name and another is the input value).

If we include one more print statement as shown below, then in the output console, program name gets printed.

printf(“%s”, argv[0]);

command line arguments in c

Example #2

Consider another example with the input value 25 to check whether the value is even or odd,

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int num;
    num = atoi(argv[1]);
    if(num % 2 == 0)
        printf("%d is Even",num);
    else
        printf("%d is Odd",num);
}

Output: 25 is odd

Explanation: The input ASCII value is stored in argv[1]. It is then converted to an integer value and checked whether it is even number or odd number. The output for the above program is “25 is Odd”.

Example #3

Another example with the input Hello World,

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    printf("Number of arguments: %d\n", argc);
    printf("Arguments entered: %s\n", argv[1]);
}

Output: 

command line arguments programs in c

Explanation:

Since the input is Hello World, the number of arguments is 3. This is because apart from the program name, each space-separated input value is considered as a separate argument and hence hello and world are counted as 2 separate arguments. 

But if you have seen, in the console window, the arguments entered shows only Hello although we have entered hello world. This is because, whenever there are space-separated arguments to be entered, the input should be given in double quotes this way —-> “Hello World”. If this case, you will see the output as shown below.

command line arguments in c with examples

Example #4

Consider another example with the input float value 4.5

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
        int x = atoi(argv[1]);
        printf("%d\n", x);
        return 0;
}

Explanation: In this case the output is 4 and not 4.500000. Since we have used atoi function, the input float value gets converted to integer value 4 and gets printed.

But the actual output is 4.500000 (float has 6 precision digits). So, to convert ASCII value to float value we need to use atof function as shown below.

float x = atof(argv[1]);

Quick Summary of Command Line Arguments in C

  • Command line arguments are passed to the main function.
  • argc denotes the number of arguments passed and argv is a character pointer array which points to the arguments passed.
  • argv[0] holds the name of the program and the name of the program is a default argument that gets counted always.
  • argv[1] points to the first command-line argument that is passed and argv[n] points the last command line argument
  • argv[argc] is a NULL pointer.
  • If an argument that you want to pass has a space, then you can pass such arguments by putting them inside double quotes “ ”. Also, when separated by space, the arguments get stored as 2 separate arguments.

Command Line Argument Programs in C

Here are some programs with solutions using command line arguments. These will help you understand command line arguments in C better.

FAQs regarding Command Line Arguments in C

Command line arguments are useful when you need to control your program from outside instead of hard coding the values inside the program.

argc contains the number of arguments passed to the program and argv is a character pointer array that stored the passed arguments.

The process to run command line arguments is given above.