Command Line Arguments in C Programming

Introduction

Programming can be powerfully customized with command line options without requiring changes to the code. These useful features offer control and flexibility, improving programs' capacity to adapt to a variety of activities and user requirements. Command line arguments (CLA) are a means of passing input parameters to a program when it is run from the command line in C programming. Here's a quick rundown of how C uses command-line arguments.

Executable Files and Command Line Parameters

In C, command line parameters can be passed to executable files. Command line arguments are what these settings are called. With the use of these arguments, users can alter the program's behavior without changing the source code.

main() Function with Command Line Arguments

In C, the main() function is the entry point of a program. It can take command line arguments to receive input from the user.

The standard signature of main() with command line arguments is as follows.

Syntax

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

Number of Arguments and Argument Values:

void main(argc, argv)
int argc;
char *argv[];
{
------
-----
------
}

The argc parameter contains the number of command line arguments passed to the program.

The argv parameter is an array of strings (char *argv[]) that contains the actual values of the command line arguments.

For Example

WAP to illustrate the command line arguments in C.

Source Code

#include<stdio.h>
#include<conio.h>
void main(argc,argv)
int argc;
char *argv[];
{
int i;
clrscr();
printf("\n Program to illustrate command line arguments\n");
printf("\n The Number of Arguments passed to the file were %d\n",argc);
printf("\n Values stored in the arguments vector are as follows : \n");
for(i=0;i<argc;i++)
{
printf("%d arguments contains %s\n",i,argv[i]);
}
getch();
}

Output

Argument

Command line argument

Summary

Command line arguments in C programming give users the ability to alter a program's behavior when it is run from the command line. The program's entry point is the main() function, which takes command line arguments via the parameters argc and argv. The number of command line arguments is indicated by the argc parameter, and their actual contents are contained in an array of strings called argv. argv[0] typically contains the name of the program that runs. Verifying the correct number of parameters and transforming them to the proper data types for additional processing are common use cases. Because command-line arguments enable users to enter data dynamically, C programs are more flexible than other programming languages.


Similar Articles