Computer Programming C: UNIT V: File Operations

Command Line Arguments in C Programming

An executable program that perform a specific task for operating system is called a command.

COMMAND LINE ARGUMENTS

 

Introduction

An executable program that perform a specific task for operating system is called a command. Some arguments are to be associated with the commands hence these arguments are called as command line arguments.

The command line arguments supply parameters to the program during its execution.

Command line arguments allows to pass the information when we ran the program, generally the information can be passed into the programs using main function via command line arguments. Command line arguments provides the information that follows the name of the programs and the name of the command line of the operating system.

The ANSI C definition for declaring the main() function is either.

int main()

or

int main(int argc, char** argv)

The second version allows arguments to be passed from the command line. Where, argc is an arguments counter and contains the number of parameters passed from the command line. argv is the arguments vector which is an array of pointers to strings.

main() function of a C program accepts arguments from command line or from other shell scripts by following commands.

They are; argc - Number of arguments in the command line including program name and argv[]‒ This is carrying all the arguments

In real time application, it will happen to pass arguments to the main program itself. These arguments are passed to the main() function while executing binary file from command line.

Now, we run the executable "test" along with 4 arguments in command line like below. Where,

argc  = 5

argv[0] = "test"

argv[1] = "this"

argv[2] = "is"

argv[3] = "a"

argv[4] = "program"

argv[5] = NULL

Example 1: Program for argc() and argv() functions in C

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])   /*command line arguments*/

{

if(argc!= 5)

{

       printf("Arguments passed through command line not equal to 5");

       return 1;

}

printf("\n Program name : %s\n", argv[0]);

printf("1st arg : %s\n", argv[1]);

printf("2nd arg : %s\n", argv[2]);

printf("3rd arg : %s\n", argv[3]);

printf("4th arg :  %s\n", argv[4]);

printf("5th arg : %s\n", argv[5]);

return 0;

}

OUTPUT

Program name : test

1st arg : this

2nd arg : is

3rd arg : a

4th arg : program

5th arg : (null)

Example 2: Program to illustrate command line arguments

#include <stdio.h>

#include <stdio.h>

int main(int argc, char *argr[])

{

  if(argc !=2)

  {

         printf("forgotted to type name \n");

         exit(1);

  }

  printf("Hellow %s", argv[1]);

}

EXPLANATION: The command line program as shown above can be executed from the prompt by specifying the name of the program along with arguments to that program.

Example:

c:\TC> sample munivrb ←┘

It displays

     Hellow Munivrb

The brackets which follows main function are used to supply arguments argc refers to the number of argument passed. argv[] is a pointer array which prints to each argument which is passed to main.

Example 3: To illustrate the number of arguments

#include <stdio.h>

main (int argc, char argv[])

{

if(argc ==2)

         printf("Argument supplied is %s\n", argv[1]);

else if(argc>2)

        printf("Too many arguments supplied \n");

else

        printf("One Argument expected \n");

}

EXPLANATION: The argument argv[0] is the name of the program invoked, which means that *argv[1] is a pointer to the first argument supplied and *argv[n] is the last argument. If no arguments are supplied, arge will be one. Thus for n arguments, arge will be equal to n+1. The program is called by the command line c:\TC sample arg1 ←┘

Example 4: Program using command line arguments.

 /* Program using command line arguments */

#include <stdio.h>

int main(int argc, char**argv)

{

/* Local definition */

int counter;

/* Statements */

puts("The arguments to the program are : ");

for(counter=0; counter<argc; counter++)

puts (argv[counter]);

getch();

return 0;

} /* main */

OUTPUT

The arguments to the program are:

CLA.EXE

EXPLANATION: The above program refers the command line arguments. The for loop is checks the condition and unit to argv[counter] until the condition satisfied counter <argc, then prints the argu[counter] as output.

 

Computer Programming C: UNIT V: File Operations : Tag: Computer Science : - Command Line Arguments in C Programming


Computer Programming C: UNIT V: File Operations



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation