C Programming

Command Line Argument

To run a program in C using command line argument is very important concept in C language.

Ways to Run our Program:

First, we have to know the different processes that exists to run a software:

  1. by IDE
  2. by double click
  3. by command line.

Whichever IDE you use (like Turbo CPP 3.0, Code Block), let the program that it gives the facility run our program.

Whichever program we write our source file is called, .c file. .c file need to build it to the exe file or build option that makes the exe file is our software. If we double click on a file, we request our OS to open the file.

By double click, the operating system runs our exe file. But it shows some blink or it does not stay to the output file. So, if we open our exe file through double click, we have to write getch () to show our result.

Using Command Line: By using the Command Line to run our program, we have to open command prompt. Command prompt is the simulator of DOS.

Arguments in main ():

  • Takes something nature of function
  • Actual and Formal Arguments
  • main () may take arguments
  • Who calls main ()?

Operating system calls the main ().

When OS calls the main (), it does not pass any value as an argument in the main ().

If we want to pass value as an argument, we do this through command line.

In command prompt,

Whenever, OS calls the main () with argument, we have to build the formal argument in main () parenthesis. Here, we passed three arguments (test 1, 3, 5.) They are individual strings.

Programming Example 1:

#include <stdio.h>

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

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

Output:

Explanation:

Block diagram of Argc and Argv []:


In command line, we pass three arguments, test 1, 3, 5. So, the total arguments are 3. This value is received by the formal argument argc in main ().

*argv [] is an array of pointer. It means it is an array that contains pointer variable of char type. For this we declare it as a character.

As, we passed 3 arguments, the number of blocks in argv [] present 3.

argv [0] -> “test.exe

argv [1] ->3

argv [2] ->5

They are Formal Arguments passing in main ().

If we run this program through command line, it shows the three strings, test1.exe, 3, 5.

You can pass arguments to main () only when you are calling your program using Command Line.

Programming Example 2:

Here we will see another example of command line argument.

#include <stdio.h>

int main( int argc, char *argv [] )
{
printf(" \n The Program's name is= %s \t", argv[0]);

if( argc == 2 )
    {
printf("\n value given by the user is= %s \t", argv[1]);
    }
    else if( argc> 2 )
    {
printf("\n Manny values provided.\n");
    }
    else
    {
printf(" \n At least one single value expected by the user !.\n");
    }

Output:

Explanation:

In this programming example, we will pass three values inside the parenthesis of main () function. The values are 1, 3, 4. Our operating system calls the main () function. At that time, we will pass the values. Then we will get some condition to print the values.

Programming Example 3:

Here we will see some more implementation of the command line arguments:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])    // passing some values inside the main () function.
{
    Int x,y, result;
    char op;

    if(argc!=4)
    {
        printf("Wrong Choice !! try again\n");
        return -1;
    }

    //get values from user
    x = atoi(argv[1]);
    y = atoi(argv[3]);

    //get operator from user
    op=argv[2][0];

    //calculate according to operator
    switch(op)
    {
        case '+':
            result=x+y;
            break;
        case '-':
            result=x – y;
            break;
        case '*':
            result=x*y;
            break;
        default:
            result =0;
            break;
    }

    If(op=='+' || op=='-' || op=='*')
        printf("Result: %d %c %d = %d\n",x,op,y,result);
    else
        printf("Operand does not exist !! \n");

    return 0;
}

Output:

Explanation:

In this programming example, we will pass some values inside the parenthesis of main () function. The values are operated to get the result depending on the user’s value that are passed inside the main() function. Our operating system calls the main () function. At that time, we will pass the values. Then we will get some condition to print the values.

Programming Example 4:

Here is the last implementation of command line arguments:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])
{
    int x,y;
    int sum;
    if(argc!=3)
    {
        printf("please use 2 values only \n");
        return -1;
    }

    x = atoi(argv[1]);
    y = atoi(argv[2]);
    sum = x+y;

    printf("Sum of %d, %d is: %d\n",x,y,sum);

    return 0;
}

Output:

Explanation:

In this programming example, we will pass three values inside the parenthesis of main () function. The values are operated by the switch case to get addition or subtraction or multiplication result depending on the user’s choice. Our operating system calls the main () function. At that time, we will pass the values. Then, we will get some condition to print the values.

Conclusion:

This article is a discussion on the concept of command line argument. We have come to the conclusion that it is a very important aspect of the technical view to run a software product. We have to know every process to run a software especially with the command line argument.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com