Chapter Summary

I l @ ve RuBoard

Chapter Summary

A C string is a series of chars terminated by the null character, `\0' . A string can be stored in a character array. A string can also be represented with a string constant , in which the characters , aside from the null character, are enclosed in double quotation marks. The compiler supplies the null character. Therefore, "joy" is stored as the four characters j , o , y , and \0 . The length of a string, as measured by strlen() , doesn't count the null character.

String constants can be used to initialize character arrays. In pre-ANSI C, only external and static arrays can be initialized , but ANSI C permits automatic arrays to be initialized, too. The array size should be at least one greater than the string length to accommodate the terminating null character. String constants can also be used to initialize pointers of type pointer-to- char .

Functions use pointers to the first character of a string to identify which string to act on. Typically, the corresponding actual argument is an array name , a pointer variable, or a quoted string. In each case, the address of the first character is passed. In general, it is not necessary to pass the length of the string because the function can use the terminating null character to locate the end of a string.

The gets() and puts() functions fetch a line of input and print a line of output, respectively. They are part of the stdio.h family of functions.

The C library includes several string-handling functions. Under ANSI C, these functions are declared in the string.h file. The library also has several character-processing functions; they are declared in the ctype .h file.

You can give a program access to command-line arguments by providing the proper two formal variables to the main() function. The first argument, traditionally called argc , is an int and is assigned the count of command-line words. The second argument, traditionally called argv , is a pointer to an array of pointers of char . Each pointer-to- char points to one of the command-line argument strings, with argv[0] pointing to the command name, argv[1] pointing to the first command-line argument, and so on.

The atoi() , atol() , and atof() functions convert string representations of numbers to type int , long , and double forms, respectively. The strtol () , strtoul () , and strtod () functions convert string representations of numbers to type long , unsigned long , and double forms, respectively.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net