What is a command line argument in C language?


An executable instruction that performs a task for OS is called a command. These commands are issued from the prompt of OS.

The arguments that are associated with the commands are as follows −

  • argc - argument count.

  • argv - argument vector.

argc − It holds the total number of arguments passed from the command prompt.

argv − It is a pointer to an array of character strings contains names of arguments.

For example,

c: |> sample. Exe hello how are you
   arguments

Here,

  • argc = 5

  • argv[0] = sample.exe

  • argv[1] = hello

  • argv [2] = how

  • argv[3] = are

  • argv[4] = you

Example

Following is the C program for command line argument −

#include<stdio.h>
main ( int argc, char *argv[ ]){
   int i;
   clrscr( );
   printf (" no. of arguments at command p = %d", argc);
   printf (" arguments given at prompt are 
");    for ( i = 1; i <argc; i++)       printf ("%s
", argv[i]);    getch( ); }

Output

To run a C program with command-line arguments −

  • Compile the program

  • Run the program

  • Go to the command prompt and give the input as shown below.

c:|> sample.exe hello how are you.
No. of arguments given at prompt is = 5
Arguments given at command prompt are:
hello
How

Are
You

Updated on: 08-Mar-2021

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements