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

Advanced C 1992

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

Part III • Working with Others

Returns

The number of bytes read or 0 at the end-of-file if the

 

function was successful. Returns -1 if the function was

 

not successful.

 

errno: EBADF

The float.h File (ANSI)

The floating-point header file, float.h, includes important information about the implementation’s floating-point capabilities. This header file was discussed in Chapter 2, “Data Types, Constants, Variables, and Arrays.”

The float.h header file is important to any program that uses floating-point math.

As the following list shows, most of the float.h header file deals with various constants. You can use these constants if necessary. For example, I use the FLT_MIN value to indicate a value that is missing (which is different from a zero value in my program).

DBL_DIG

For a data type of float, the number of

 

decimal digits of precision

DBL_EPSILON

For a data type of float, the smallest value

 

such that 1.0 + DBL_EPSILON != 1.0

DBL_MANT_DIG

For a data type of double, the number of bits

 

in the mantissa

DBL_MAX

For a data type of double, the maximum value

DBL_MAX_10_EXP

For a data type of double, the maximum

 

decimal exponent

DBL_MAX_EXP

For a data type of double, the maximum

 

binary exponent

DBL_MIN

For a data type of double, the minimum

 

positive value

DBL_MIN_10_EXP

For a data type of double, the minimum

 

decimal exponent

506

 

All About Header Files

C C C

 

 

13C

 

 

C C C

DBL_MIN_EXP

For a data type of double, the minimum

C C

 

 

binary exponent

 

FLT_DIG

For a data type of float, the number of

 

 

decimal digits of precision

 

FLT_EPSILON

For a data type of float, the smallest value

 

such that 1.0 + FLT_EPSILON != 1.0

 

FLT_MANT_DIG

For a data type of float, the number of bits in

 

the mantissa

 

FLT_MAX

For a data type of float, the maximum value

FLT_MAX_10_EXP

For a data type of float, the maximum

 

 

decimal exponent

 

FLT_MAX_EXP

For a data type of float, the maximum binary

 

exponent

 

FLT_MIN

For a data type of float, the minimum

 

 

positive value

 

FLT_MIN_10_EXP

For a data type of float, the minimum

 

 

decimal exponent

 

FLT_MIN_EXP

For a data type of float, the minimum binary

 

exponent

 

FLT_RADIX FLT_ROUNDS LDBL_DIG

For a data type of float, the exponent radix For a data type of float, addition rounding

For a data type of long double, the number of decimal digits of precision

LDBL_EPSILON

For a data type of long double, the smallest

 

value such that 1.0 + LDBL_EPSILON != 1.0

LDBL_MANT_DIG

For a data type of long double, the number of

 

bits in the mantissa

LDBL_MAX

For a data type of long double, the maximum

 

value

LDBL_MAX_10_EXP

For a data type of long double, the maximum

 

decimal exponent

507

Part III • Working with Others

LDBL_MAX_EXP

For a data type of long double, the maximum

 

binary exponent

LDBL_MIN

For a data type of long double, the minimum

 

positive value

LDBL_MIN_10_EXP

For a data type of long double, the minimum

 

decimal exponent

LDBL_MIN_EXP

For a data type of long double, the minimum

 

binary exponent

The io.h File

The io.h header file supplements the stdio.h header file of ANSI C. The file, as shown in this book, is used with various Microsoft versions of C. The io.h header file contains various low-level I/O function prototypes.

The limits.h File (ANSI)

All parts of the C language contain limits. Many of the limits that apply to floatingpoint math are in the float.h header file. The integer limits are in the limits.h header file and are shown in the following list:

CHAR_BIT

The number of bits in a char

SCHAR_MIN

The minimum signed char value

SCHAR_MAX

The maximum signed char value

UCHAR_MAX

The maximum unsigned char value

CHAR_MIN and SCHAR_MIN

The minimum char value

CHAR_MAX and SCHAR_MAX

The maximum char value

MB_LEN_MAX

The maximum number of bytes in a

 

multibyte char

SHRT_MIN

The minimum (signed) short value

508

All About Header Files

C C C

 

13C

 

C C C

 

C C

SHRT_MAX

USHRT_MAX

INT_MIN

INT_MAX

UINT_MAX

LONG_MIN

LONG_MAX

ULONG_MAX

The maximum (signed) short value

The maximum unsigned short value

The minimum (signed) int value

The maximum (signed) int value

The maximum unsigned int value

The minimum (signed) long value

The maximum (signed) long value

The maximum unsigned long value

Rather than hard code numeric values for these values, always use the identifiers.

The locale.h File (ANSI)

There are more ways to tell the time than there are hours in a day. Many countries have a special way of writing the time and date, and sometimes one country has more than one way to specify the time or date.

The locale.h header file defines how time and dates are formatted for thecurrent country. You can set the locale by calling the C setlocale() function, with one of the parameters in Table 13.5 as the first parameter. The second parameter is either the character string "C" (for United States defaults) or " " for the native country’s defaults. Any other string for the second parameter depends on the implementation of the compiler.

Table 13.5. Locale identifiers used with the lconv structure.

Identifier Description

LC_ALL

Sets all categories.

LC_COLLATE Sets the collate order. Used by strcoll() and strxfrm()

LC_CTYPE Sets the character set queries for ctype.h

LC_MONETARY Sets the display format for currency. No C functions use this information.

continues

509

Part III • Working with Others

Table 13.5. continued

Identifier

Description

LC_NUMERIC

Sets the display format for numbers. Used by printf() and

 

scanf() type functions.

LC_TIME

Sets the display format for the time. This affects strftime(),

 

ctime(), and asctime().

 

 

The localeconv() function returns a pointer to the internal lconv structure, which contains the formatting information. The format and contents of lconv depend on the compiler, and as such are not documented here.

Although you can get a pointer to the lconv structure, you must use the setlocale() function to modify it rather than modify it directly. This limits your direct use of thestructure to readonly. Savetheinformation pointedtoby localeconv() in a buffer in your program because the structure pointed to by localeconv() may change or move.

The malloc.h File

Your system may have a header file called malloc.h. This header supplements the stdlib.h header file that is part of ANSI C. The malloc.h header file contains functions, identifiers, and other things that deal with memory allocation.

The math.h File (ANSI)

The math.h header file defines prototypes for the various floating-point functions. The math.h file and the errno.h file are often included with the float.h header file.

510

All About Header Files

C C C

 

13C

 

C C C

 

C C

The memory.h File

The memory.h header file contains function prototypes for the memory functions and buffer manipulation functions. Many ANSI C compilers include these functions in the string.h header file. See Table 13.6.

Table 13.6. Memory functions.

Function Description

memchr() Returns a pointer to the first byte in a block matching the specified character

memcmp()

memcpy()

memmove()

Compares two blocks of memory, returning any differences

Copies two nonoverlapping blocks of memory

Copies two blocks of memory, which may or may not overlap

memset() Fills a block of memory with the specified byte

The memory functions are used in much the same way as the string functions. Memory functions, however, work with a specific length; string functions expect a terminating NULL to mark the end of the string.

The search.h File

The search.h header file contains function prototypes for the search and sort routines. Many ANSI C compilers contain these functions in the stdlib.h header file. The search function, bsearch(), performs a binary search. The sort function, qsort(), performs a quick sort. Both functions require a user-written compare function.

511

Part III • Working with Others

The setjmp.h File (ANSI)

The setjmp() and longjmp() functions are defined in the setjmp.h header file. The ANSI C standard defines setjmp() as a macro. However, some compiler producers have written setjmp() as a function.

The signal.h File (ANSI)

ANSI C uses a signal to tell the program that an error has occurred. The signal.h header file defines the conditions that are trapped and optionally passed to the program. See Table 13.7.

Table 13.7. Signal values.

Value Description

SIGINT

SIGILL

An interrupt, such as a Ctrl-C keypress. Required by ANSI.

An illegal instruction or an anomaly in the program’s execution that is usually caused by corrupted memory. Required by ANSI.

SIGFPE

A floating-point error or another math error exception. Required by ANSI.

SIGSEGV

An attempt to access memory was made that the program does not allow. Required by ANSI.

SIGTERM

The program was requested to end (probably caused by the operating system shutting down). Required by ANSI.

SIGBREAK

A Ctrl-Break sequence.

SIGABRT

Abnormal termination, perhaps triggered with a call to

 

abort(). Required by ANSI.

 

 

The two functions that deal with signaling are raise(), which triggers a signal condition, and signal(), which sets up the handler for the error condition.

512

All About Header Files

C C C

 

13C

 

C C C

 

C C

The stdarg.h File (ANSI)

The stdarg.h header file has been designed to replace the varargs.h header file. Generally, you cannot mix these two header files in the same source file.

An important part of C programming is the capability to use a variable number of arguments. Just think of how printf() or scanf() would work without this capability.

If your program uses the stdarg.h header file, a function with a variable number of arguments must have at least one fixed (always present) argument. This fixed argument is passed to the va_start() routine to enable access to the variable arguments.

The program in Listing 13.1, VARGS.C, shows two ways to use a variable number of arguments. The OurErrors() function shows the use of vfprintf() as well. Because the vfprintf() is new, many programmers do not fully understand it yet.

Listing 13.1. VARGS.C

/*

VARGS, written 1992 by Peter D. Hipson */

/*

This program demonstrates stdarg.h

*/

#include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h>

#define

TRUE

1

#define

FALSE

(!TRUE)

int

AddList(int nFirst, ...);

int

OurErrors(char * OutputFormat, ...);

void

main()

 

{

 

 

int

nSum;

 

continues

513

Part III • Working with Others

Listing 13.1. continued

nSum = AddList(10, 20, 30, 40, 50, 60, 70, 80, 90, INT_MIN);

 

(void)OurErrors("%s - %d, %s\n", "First", nSum, "Second");

}

 

int

AddList(

 

int nFirst,

 

...)

{

 

int

nReturnValue = nFirst;

int

nThisValue;

va_list Arguments;

va_start(Arguments, nFirst);

while((nThisValue = va_arg(Arguments, int)) != INT_MIN)

{

 

nReturnValue += nThisValue;

 

}

 

va_end(Arguments);

 

return(nReturnValue);

}

 

int

OurErrors(

 

char * OutputFormat,

 

...)

{

 

va_list Arguments;

va_start(Arguments, OutputFormat);

514

All About Header Files

C C C

 

13C

 

C C C

 

C C

vfprintf(stderr, OutputFormat, Arguments);

va_end(Arguments);

return(0);

}

The stddef.h File (ANSI)

The stddef.h header file is one of the more important header files for the C compiler. This header file contains many common constants, identifiers, types, and variables, for example, NULL and offsetof(). The NULL identifier is defined as zero or as a pointer that is guaranteed to point to nothing. The offsetof() macro determines the offset from the beginning of a structure to a given member. Most nontrivial programs should include stddef.h.

The stdio.h File (ANSI)

Most of the higher level I/O functions are in the stdio.h header file. This file is used in most (if not all) programs, because a program without I/O is rather uncommunicative!

One item defined in stdio.h is the structure that the FILE* type points to. Note, however, that you should never modify this structure’s members.

Also defined in stdio.h are the constants that fseek() uses, including SEEK_CUR (the current file position), SEEK_END (the end of the file), and SEEK_SET (the beginning of the file).

The stdio.h header file also contains the prototypes for the various I/O functions and the standard file identifiers—stdin, stdout, stderr, stdaux, and

stdprn.

515