¤ Home » Programming » C Tutorial » Pointers in C - Part 8 of 9

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"); 
	if((m = atoi(argv[2])) == 0)
	   printf("\nThe second argument needs to be a number\n\n");

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

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

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


comments powered by Disqus

Download Mobile App for Android

Web Service Offers

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.

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 XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.

Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy