15.9 – Command Line Arguments

by subbu on September 3, 2014

Command Line arguments in C language

In Turbo C

As we have discoursed earlier under 12.21.Creating executable file in Turbo C, we can create an executable file and can be executed from command prompt.

While calling executable file from the command line, we can send other parameters to the program along with the program name. These parameters can be accessed from the main(). To receive the arguments from command line the structure of main() must be changed as

int main(int n, char *s[])
{

}

Here “n” get the count of arguments sent from the command line including with the file name and s[] would be the pointer array which refers command line arguments.

Now let us see what actually happens when we send arguments through the command line. While executing the program, the operating system converts the arguments as an array of strings and sends the count of arguments and the references of array of strings as arguments to the main(). Here the first argument would be the name and path of executable file.

By using the reference of array of strings we can access the command line arguments from the main() using a loop.

Command line arguments

Example explained:

  1. Sending command line arguments to the program
  2. The Os automatically creates an array of strings with command line arguments.
  3. Number of argument and the reference of array of strings are sending as arguments to the main
  4. main is accessing the command line arguments.

Program:

step1: type the program and save with the name “demo.c”

#include<stdio.h>
int main(int n, char *x[])
{
int i;
printf("Command line arguments are:\n");
   for(i=0;i<n;i++)
printf("%s\n",x[i]);
return 0;
}

Writing C program

Saving a C program

step2: Compile the program by pressing F9

Compiling a C program

step3: Go to the command prompt (OS shell) by selecting file – DOS/OS Shell

Opening OS Shell

step4: Send command line arguments while executing the program

Passing command line arguments

In Ubuntu Linux gcc:

Step1: Type the program in the text editor

$gedit demo.c
#include<stdio.h>
int main(int n, char *x[])
{
int i;
printf("Command line arguments are:\n");
for(i=0;i<n;i++)
   printf("%s\n",x[i]);
return 0;
}

Save the program and Quit (Ctrl+S, Ctrl+Q)

Typing a C program in Linux gedit

Step2: Compile the program

$gcc –o demo demo.c
$

Step3: Execute the program

$./demo live one day at a time
Command line arguments are:
./demo
live
one
day
at
a
time
$

Specification: Send two integers as command line arguments and print the sum of them

$gedit sum.c
#include<stdio.h>
int str2int(char*);
int main(int n,char *x[])
{
int a,b,c;
a=str2int(x[1]);
b=str2int(x[2]);
c=a+b;
printf("Sum of two numbers %d",c);
return 0;
}
int str2int(char *s)
{
int i,n;
for(n=0,i=0;s[i]!='\0';i++)    /* extracting characters */
n=n*10+(s[i]-'0');             /* converting and concatenating to number */
return n;
}

Save and exit (Ctrl+S, Ctrl+Q)

$gcc –o sum sum.c
$./sum 450 230
Sum of two numbers : 680
$

Previous post:

Next post: