Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
96
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

Part III • Working with Others

The stdlib.h File (ANSI)

The stdlib.h header file contains the definitions and declarations for commonly used library functions. These library functions are usually found in the stdlib.h header file because they do not fit elsewhere or cannot be placed in other header files for technical reasons. This section describes some of the functions in the stdlib.h header file.

String Conversion

The string conversion functions convert strings to their numeric equivalent. The conversion process is performed in accordance with the locale settings, so that the thousands separator (a comma in the United States) may be recognized. The three ANSI functions for string conversion are strtod(), strtol(), and strtoul().

Memory Allocation

If a compiler does not have a separate memory allocation header file (such as malloc.h), the memory allocation functions are defined in stdlib.h.

Random Numbers

The C random number function is called rand(). The random numbers may be seeded using srand(). The sequence of random numbers repeats, however, if srand() receives the same seed each time. A common trick is to seed the random numbers with the current time or the elapsed time between two (or more) keystrokes.

Communications with the Operating System

You can issue operating system commands with the system() function. This function’s behavior is not defined by ANSI C, with two exceptions. If the system() function is called with a NULL parameter, it returns TRUE if a command interpreter is present. If the system() parameter is called with NULL and signal() returns zero, you cannot issue commands to the operating system.

516

All About Header Files

C C C

 

13C

 

C C C

 

C C

You can use the atexit() function to tell the operating system which functions should be called when the program ends. Although you can include functions, they cannot be removed after installation.

You can use abort() to end your program. Regardless of the use of the SIGABORT signal error handler, abort() will not return to the caller.

Search Functions

The bsearch() function is often defined in stdlib.h. Why bsearch() is defined here and qsort() is not is beyond me.

Integer Math

ANSI C has two integer math functions. These functions enable you to divide two integer values, and provide both a quotient and a remainder in one call. As well, these functions produce more predictable results than the division operator.

Multibyte Characters

Because ANSI C supports multibyte character sets (useful for languages that do not use the roman character set), there are functions to support character sets that have more than 256 characters. These characters are represented with a two-byte sequence, and their values depend on both the compiler and the language. You must read your compiler’s documentation to determine which functions are provided and how to use them.

The string.h File (ANSI)

The various string manipulation functions are defined in the string.h header file. This file also contains various memory functions (such as the ones found in the memory.h header file).

517

Part III • Working with Others

The time.h File (ANSI)

The time functions are in the time.h header file. ANSI C added the strftime() function to the C language, which helps you format a time string.

The varargs.h File

In most C compilers, the varargs.h header file has been replaced with the stdarg.h header file. You usually cannot mix these two header files in the same source file.

Summary

In this chapter, you learned about the ANSI standard header files.

Although there are standards for header files, most compilers add header files that offer additional functionality (such as graphics) or split the functionality of existing ANSI header files into several new files (such as Microsoft’s use of the memory.h header file).

The varargs.h and the stdarg.h files both define the passing of a variable number of arguments to a function. In general, the stdarg.h header file is considered the ANSI standard way to pass a variable number of arguments.

518

Part IV

Documenting the

Differences

519

Advanced C

520

CANSI ’s LibraryCFunctionsCC C C

C14C C

C C C

C14C C

C C C

ANSI C’s Library

Functions

This chapter presents the ANSI C standard functions. I have not included functions that are not ANSI—there are several hundred additional non-ANSI functions that vary both in function and implementation between different compilers. For the nonANSI functions, it is preferable to use the documentation that is supplied with the compiler because there may be differences between different implementations of a non-ANSI function that you would not expect.

521

Part IV • Documenting the Differences

Functions

The ANSI C libarary functions are presented below in alphabetical order.

abort()

Header:

process.h & stdlib.h

Syntax:

void abort(void);

Description:

Aborts (ends) the current program or process.

Parameters:

None.

Returns:

Does not return to caller.

Example:

if (nDisaster)

 

{

 

abort();

 

}

Note:

Generally, use abort() only if recovery is not possible. Open files

 

will probably be lost and any work entered by the user and not

 

saved cannot be recovered.

abs()

Header:

math.h & stdlib.h

Syntax:

int abs(int nValue);

Description:

Returns the absolute value of the provided parameter.

Parameters:

nValue—A signed integer whose absolute value is desired.

Returns:

Returns the absolute value of the provided parameter.

Example:

printf(“abs(-20) = %d\n”, abs(-20));

 

/* Will print: abs(-20) = 20 */

522

ANSI C’s Library Functions

acos()

Header:

math.h

Syntax:

double acos(double dValue);

Description:

Provides the principal arc cosine of dValue.

Parameters:

dValue—Type double, ranging -1 to 1.

Returns:

Arc cosine, a value between zero and 3.1415926.

Example:

dArcCos = acos(0.4);

 

/* dArcCos will be 1.047 */

C C C

C14C C

C C C

Note: Sets errno to EDOM if the parameter passed is outside the allowed range.

asctime()

Header:

time.h

Syntax:

char * asctime(const struct tm *

 

TimePointer);

Description:

Converts time as contained in parameter TimePointer to a

 

character string. The string’s format follows the example:

 

Wed Jun 24 22:21:00 1992\n\0.

Parameters:

A pointer to a properly initialized time structure.

Returns:

A character pointer to a string. You should copy this string to your

 

program’s own memory if you wish to save it.

Example:

struct tm tmTime;

 

time_t lTime;

 

time(&lTime);

 

tmTime = localtime(&ltime);

 

printf(“Time: ‘%s’”, asctime(&tmTime);

Note:

Initialize the time structure (tm in the example) using time() and

 

localtime().

523

Part IV • Documenting the Differences

asin()

Header:

math.h

Syntax:

double asin(double dValue);

Description:

Returns the arcsine of parameter dValue.

Parameters:

dValue—Which must be in the range of -1 and 1.

Returns:

The arcsine of dValue.

Example:

dArcSine = asin(0.5);

 

/* dArcSine will be 0.5236 */

assert()

Header:

assert.h

Syntax:

void assert(unsigned nConditional);

Description:

The assert() macro prints a message.

Parameters:

nConditional—A conditional expression, such as (nCount < 0).

Returns:

No return value.

Example:

assert(nCount < 0);

Note:

If the conditional expression evaluates to 0, then a message

 

containing the conditional expression, the source filename, and

 

line number is printed to stderr, and the program ends.

atan()

Header:

math.h

Syntax:

double atan(double dValue);

Description:

Returns the arctangent of dValue.

Parameters:

dValue—The value for which arctangent is desired.

524

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

Returns:

The arctangent of dValue.

C C

 

Example:

double

dTangent

 

 

dTangent = atan(0.25);

 

Note:

Also see atan2().

 

atan2()

 

 

 

Header:

math.h

 

 

Syntax:

double atan2(double dValue1, double

 

 

 

dValue2);

 

Description:

Returns the arctangent of dValue1 and dValue2.

 

Parameters:

dValue1—The first value whose arctangent is desired.

 

 

dValue2—The second value whose arctangent is desired.

 

Returns:

The arctangent of dValue1 and dValue2.

 

Example:

double

dTangent

 

 

dTangent = atan2(0.25, 3.2);

 

Note:

Also see atan().

 

atexit()

 

 

 

Header:

stdlib.h

 

 

Syntax:

int atexit(void (* function)(void));

 

Description:

Tells the system to call function when the program ends.

 

Parameters:

function—Pointer to the function to be called.

 

Returns:

Zero if atexit() is successful.

 

Example:

atexit(OurExitFunction);

 

Note:

Functions are called in a last in, first out manner. There is no way

 

to remove a function once it has been passed using atexit().

525