Previous Table of Contents Next


3.9.1. Command-Line Arguments

When a C program is invoked, user-provided arguments may be available to it. These arguments are typically typed by the user on the command line that invoked the program, and are provided as an array of strings passed to the main() function. A main() function that wishes to receive command-line arguments is declared as

int main(int argc, char **argv)

where the parameter argc is the number of command-line arguments and argv is a reference to the array of the argument strings themselves. When passed, the strings are (as usual) represented as character pointers and the runtime startup code passes an array of these pointers to main(). Due to the special rules concerning arrays in expressions and as passed to functions (see section 3.6.5), main actually receives a pointer to the first element of the array; that is, main receives a pointer of type char **. However, main can equivalently be defined as accepting an array of pointers, in other words, as main(int argc, char *argv[]).

argv[0] is always the name of the program itself, if available (or the empty string “” otherwise). Therefore, the first command-line argument is argv[1]. A standard example is a program that simply prints out its arguments (a la the UNIX echo program):

#include <stdio.h>
int main(int argc, char **argv)
{
       int i;
       for(i = 1; i < argc; i++)
              printf(“argv[%d]: %s\n”, i, argv[i]);
       return 0;
}

It is generally straightforward for a program to parse its command-line arguments and interpret them as option switches, filenames, and the like.

3.9.2. Exit Status

A C program can return an exit status to the invoking environment. The exit status is simply an integer value, with 0 indicating successful termination and nonzero values representing other conditions. The exit status is either the return value from main or the value passed to the exit function. See section 3.10.4.5 for more information on exit and the two predefined exit status values EXIT_SUCCESS and EXIT_FAILURE.

3.10. The Standard C Library

The Standard C runtime library is an occasionally eclectic mixture of extremely useful functions, along with a few unlikely historical warts. Most of these functions are valuable, and their use is recommended for productive and efficient programming. A few (such as strncpy) have foibles that must be understood for proper use, and one or two (such as gets) cannot be used safely, and are best avoided.

Related sets of these functions are declared in several standard headers, and before any of the functions are called, it is usually important (and always a good idea) for the corresponding header(s) to be included. Besides function prototype declarations, the headers also contain definitions for several important constants (preprocessor macros), typedefs, and structures.

This section is arranged mostly in the manner of a reference manual, although it also contains guidelines for use of these functions, examples, and even a few sample implementations, for illustrative purposes. As an overview of the kinds of functionality provided, and an index into the individual headers, we can list several classes of operations, and the headers in which functions along those lines will be found:

Functionality Header(s)

basic definitions <stddef.h>
I/O & files <stdio.h>
characters and strings <string.h>, <ctype.h>, <stdlib.h>, <stdio.h>
memory allocation <stdlib.h>
mathematics <math.h>
date and time <time.h>
variable argument lists <stdarg.h>
nonlocal jumps <setjmp.h>
assertions <assert.h>
signal handling <signal.h>
error handling <errno.h>, <stdio.h>, <string.h>
minima and maxima <limits.h>, <float.h>
locale, internationalization <locale.h>, <stdlib.h>, <wchar.h>, <wctype.h>
operating system interface <stdlib.h>

It’s also worth mentioning a few categories of functionality that are not addressed by the standard C library. There are no provisions for performing character-at-a-time input from keyboards or serial devices; screen operations such as moving the cursor, setting inverse video, or clearing the screen; graphics operations such as drawing lines; filesystem operations such as creating or listing directories; or graphical user interface operations such as working with the mouse or with windows or menus. Performing any of these operations requires using functions or system calls that are unique to a particular I/O device or operating system, and although these calls can in most cases be made from C programs, nothing more can be said about them here because they are not standard.

In the following subsections, the description of each standard library function begins with a skeletal declaration, which can be thought of either as an external prototype function declaration or the first line of the actual definition. These skeletons give the argument and return types of each function, and provide parameter names that are referred to in the descriptions.


Previous Table of Contents Next