How2Lab Logo
tech guide & how tos..


Pointers in C - Part 8 of 9


So far, we have been using the main function without any argument. Now that we have learnt about pointers, it is the right time to introduce the arguments of function main().

We know that main() is the controlling function and gets executed first on invocation of a C program. The main function can also collect arguments that you can pass from the command line when invoking the C program. The passed arguments are typically collected via two parameters - argc and argv. The parameter argc is essentially the argument count, an integer quantity that identifies the number of command line arguments the program is to be invoked with. The parameter argv is an array of pointers to character (essentially, an array of strings) that hold the passed arguments (one argument per string).


The general syntax of invoking a C program with command line arguments is:

<program_name> <argument 1> <argument 2> ... <argument n>

Let us illustrate the usage of command line arguments with an example. Consider a C program extract that is supposed to extract n characters from a string starting from position m. The program can be invoked with the 3 arguments, viz. string, m, and n as per the following syntax:

C:\> extract <string> <m> <n> 
 
Example actual usage:
C:\> extract "Sound of Music" 10 5

Note: C:\> denotes the command prompt, which may vary from one computer to another.

Here the first argument of main, viz. argc gets a value 4. Note that even the program name is treated and counted as an argument. The second argument of main, viz. argv, is also referred as argument vector and is essentially an array of pointers to character. This second argument will get initialized with the arguments themselves that are passed as strings. Thus argv receives the following values - extract, Sound of Music, 10 and 5. All values are received as string. See the memory representation below for argv:

Here is the full program:

#include <stdio.h>
main(int argc, char *argv[])
{
	int i, len, m, n;

	if(argc != 4)
	{ 
	   printf("\nUsage: extract <string> <offset> <no_of_chars>\n"); 
	   exit();
	}
	
	if((m = atoi(argv[2])) == 0)
	{
	   printf("\nThe second argument needs to be a number\n\n");
	   exit();
	}

	if(( n = atoi(argv[3])) == 0)
	{ 
	   printf("\nThe third argument needs to be a number\n\n");
	   exit();
	}

 	len = strlen(argv[1]);
 	if(m > len)
 	{ 
	   printf("\n The starting position %d is greater than string width.\n\n", m); 
	   exit();    
	}

	printf("\nThe string extracted is: ");
	
	for(i=0; *(argv[1] + m - 1) != '\0' && i < n; m++, i++)
		printf("%c", *(argv[1] + m -1));
	printf("\n");
}


Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide | Hostinger
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.

Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.