Command Line Argument in C| Basic IT Topic

What is Command Line Argument in C

command line argument in c

COMMAND LINE ARGUMENT:-

Command line argument in C is a mechanism by which a program takes input during runtime.
The main function takes two parameters in a command line argument such as

Argument Counter:- It counts the number of argument on the command line.

Argument Vector:- Is a pointer array which points to each argument passed to the program.

  • When we  developing an application for DOS operating system in that case command line arguments  are used
  • All parameters in the command line argument are by default string type.
  • It is basically used for control the program from outside.
  • The arguments are passed to the main method.
  • Command line argument programs are not executed directly in TC IDE because arguments are cannot be passed.

Syntax:-

main(int argc, char*argv[])

Ex:-
main(int argc, char*argv[])  //argc=argument counter and argv=argument vector.
{

}

If no parametr are given in main function bodefault it is void types .It means program doesn’t take any inout through command line.

PROGRAM:-
DATA STORE AND DISPLAY IN COMMAND LINE AGRUMENT 
main(int argc,char * argv[])
{
int a =atoi(argv[1]);
printf(" The data is %d",a);
}

O/P- ./a.out 12
The data is 12
PROGRAM:-
ADDITION OF TWO NUMBER
#include "stdio.h"
main(int argc,char*argv[])
{
int a=atoi(argv[1]);
int b=atoi(argv[2]);
int c=a+b;
printf(" The data is %d",&c);
}
O/P:- ./a.out 2 4
The data is 6
PROGRAM:-
DISPLAY THE NUMBER FROM THE GIVEN RANGE
main(int argc,char *argv[])
{

int i=atoi(argv[1]);
int j=atoi(argv[2]);
while(i<=j)
{
printf("%d \n",i);
i++;
}
}
O/P:-
./a.out 1 7

1
2
3
4
5
6
7

Some important points on Command Line Argument:-

In command line argument process uses argument counter, argument vector and environment vector .

Space is used as separator from one parameter to another parameter in command line argument.

Every parameter in command line argument is string types

Argument vector and Environment vector is an array of string types

argc,argv and env are used generally in command line argument

 

Also Read:- What is C?

Also Read:-Pointer in C

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community