Explain command line arguments in C, C++ with real scenario

Command line arguments in C, C++ programs supply values from outside of a program. The same concept of command line argument is applied to all the languages  for example, c, c++,c# and java etc. Also, command line arguments are applicable to scripts also.

Some time we get a requirement in a real time application to supply some values to a C or C++ program from outside e.g. from a command line. Then we use command line arguments.

In another word, we may want to provide some parameter e.g. path of the file, name of a server or IP address and port etc. to a program (executable) then run the application. The option is to pass these parameters to main() function of an executable as an command line arguments.

Command line arguments or values will be received by a program in function main() from where the application starts in C, C++ etc.

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

By receiving these command line arguments  in main() function, we use these arguments inside the program.

ARTUMENTS DESCRIPTION:

int argc : This paramete is the number of arguments recieved. Note that if you supply 2 arguments,argc would be of count 3 as application name itself would be counted as 1, hence, total number of arguments will be 3.

char* argv[]: In this array of charactor pointers, we receive command line argumemnts supplied from command line. argv[0] will always have application name by default. So, whatever arguments we will supply need to fetch from argc[1], argc[2]…etc.

Let’s say we have an executable called “test.exe” and want to supply two command line arguments say “filename” and “serverlocation”. On windows command console, we can go to the directory where exe resides and run it as follow

Test.exe              filename              serverlocation

On Linux terminal we use like this

./Test.exe           filename              serverlocation

Command Line Arguments in C Example:

// command line arguments sample

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

	char* ApplicationName = NULL;
	char* fileName = NULL;
	char* server = NULL;
	// Check if desired number of arguments has been sent
	// from user or else return informing arguments required.
	if (argc != 3)
	{
		printf("These arguments need to be supplied by user to main application: appname filename server\n");
		return 1;
	}

	// store arguments in variable here for further use.

	//argv[0] always store name of the application that you don't have to supply.	

	ApplicationName = argv[0];
	fileName = argv[1];
	server = argv[2];    

	printf("Application:%s\n",ApplicationName);
	printf("FileName:%s\n",fileName);
	printf("Server:%s\n",server);


	return 0;
}

Command line arguments – Real Time Scenario Example:

Suppose, we want to create a custom download manager which can download file from multiple server locations to the local system. So, to download files we need to supply the file name and server location to binary application. e.g.

DownloadManager.exe                                filename              serverlocation

We could have hard coded the filename and server location in the program if there was only fixed file name and server location. But, we need a flexibility i.e. supply any filename and any server location and download files on local system on the fly.

So, here we should go for command line arguments.

NOTES:

  • Note that values we supply to a program is called command line arguments in C and C++ programming. It does not mean that only we can use command line console to execute a program on windows or terminal on Linux. But, in any program, in the code, also we can use this mechanism as we may want to call another program with command line arguments from a program.
  • For command line arguments, same concept is used for any languages i.e. C, C++, C#, Java and Scripting etc. Just, syntax can be different.

Related Posts