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

Advanced C 1992

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

Part IV • Documenting the Differences

gets()

Header:

stdio.h

Syntax:

char * gets(char * szBuffer);

Description:

Gets the next line from stdin, until either the end of the stdin file

 

is reached or until a newline character is encountered.

Parameters:

szBuffer—Pointer to a character string to hold the characters

 

read.

Returns:

Pointer to szBuffer or NULL if an end of file condition is encoun-

 

tered.

Example:

char szBuffer[100];

 

gets(szBuffer);

Note:

Careful: There is no check for buffer overrun! It may be better to

 

use fgets() rather than this function.

gmtime()

Header:

time.h

 

Syntax:

struct tm * gmtime(const time_t *

 

 

TimeValue);

Description:

Breaks down TimeValue and places the result into the tm structure.

Parameters:

TimeValue—Pointer to a time_t variable.

Returns:

A returned pointer to a structure of type tm.

Example:

tm

TimeStruct;

 

time_t

OurTime;

 

OurTime = time(NULL);

 

TimeStruct = (tm*)gmtime(&OurTime);

Note:

Remember to consider the effects of different time zones and

 

daylight savings time.

546

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

isalnum()

 

C C

 

 

Header:

ctype.h

 

Syntax:

int isalnum(int Character);

 

Description:

Tests to see if the specified character is an alphanumeric character

 

(a–z, A–Z, or 0–9).

 

Parameters:

Character—The character to be tested.

 

Returns:

A non-zero if the character is alphanumeric or a zero if it is not.

Example:

if (isalnum(‘a’))

 

 

printf(“‘a’ is alphanumeric \n”);

 

Note:

See isalpha().

 

isalpha()

 

 

Header:

ctype.h

 

Syntax:

int isalpha(int Character);

 

Description:

Tests to see if the specified character is an alphabetic character

 

(a–z or A–Z).

 

Parameters:

Character—The character to be tested.

 

Returns:

A non-zero if the character is alphabetic or a zero if it is not.

Example:

if (isalpha(‘a’))

 

 

printf(“‘a’ is alphabetic \n”);

 

Note:

See isalnum().

 

iscntrl()

 

 

Header:

ctype.h

 

Syntax:

int iscntrl(int Character);

 

Description:

Tests to see if the specified character is a control character

 

(‘\x00’–‘\x1f ’).

 

547

Part IV • Documenting the Differences

Parameters:

Character—The character to be tested.

Returns:

A non-zero if the character is a control character or a zero if it is

 

not.

Example:

if (iscntrl(‘\a’))

 

printf(“‘\a’ is a control character”

 

“ (the bell?) \n”);

Note:

See isalpha().

isdigit()

Header:

ctype.h

Syntax:

int isdigit(int Character);

Description:

Tests to see if the specified character is a numeric digit (0–9).

Parameters:

Character—The character to be tested.

Returns:

A non-zero if the character is a numeric digit or a zero if it is not.

Example:

if (isdigit(‘1’))

 

printf(“‘1’ is a digit \n”);

Note:

See isalnum().

isgraph()

Header:

ctype.h

Syntax:

int isgraph(int Character);

Description:

Tests to see if the specified character is a printable character

 

(except a space).

Parameters:

Character—The character to be tested.

Returns:

A non-zero if the character is printable, or a zero if it is not.

Example:

if (isgraph(chChar))

 

putc(chChar);

Note:

To test for a blank character, use isspace().

548

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

islower()

 

C C

 

 

Header:

ctype.h

 

Syntax:

int islower(int Character);

 

Description:

Tests to see if the specified character is lowercase (a–z).

 

Parameters:

Character—The character to be tested.

 

Returns:

A non-zero if the character is lowercase or a zero if it is not.

 

Example:

if (islower(chChar))

 

 

putc(chChar);

 

Note:

Also see isupper().

 

isprint()

 

 

Header:

ctype.h

 

Syntax:

int isprint(int Character);

 

Description:

Tests to see if the specified character is a printable character

 

(including spaces).

 

Parameters:

Character—The character to be tested.

 

Returns:

A non-zero if the character is printable, or a zero if it is not.

Example:

if (isprint(chChar))

 

 

putc(chChar);

 

Note:

To test for all characters except for a blank character, use

 

isgraph().

 

ispunct()

 

 

Header:

ctype.h

 

Syntax:

int ispunct(int Character);

 

Description:

Tests to see if the specified character is valid punctuation, such as

 

the period (.), comma (,), or exclamation point (!).

 

549

Part IV • Documenting the Differences

Parameters:

Character—The character to be tested.

Returns:

A non-zero if the character is punctuation or a zero if it is not.

Example:

if (ispunct(chChar))

 

putc(chChar);

Note:

None.

isspace()

Header:

ctype.h

Syntax:

int isspace(int Character);

Description:

Tests to see if the specified character is a valid whitespace

 

character.

Parameters:

Character—The character to be tested.

Returns:

A non-zero if the character is a valid whitespace character or a zero

 

if it is not.

Example:

 

 

if (isspace(chChar))

 

putc(chChar);

Note:

To test for a non-blank character, use isgraph(). Valid whitespace

 

characters include those shown in Table 14.3:

Table 14.3. Valid whitespace characters.

 

 

Character

Description (hex value)

 

 

‘ ‘

‘\f’

‘\n’

‘\r

‘\t

‘\v

The space character.

The form feed character (\x0C’). The newline character (\x0A’).

The carriage return character (\x0D’). The tab character (\x09’).

The vertical tab character (\x0B’).

550

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

isupper()

 

C C

 

 

Header:

ctype.h

 

Syntax:

int isupper(int Character);

 

Description:

Tests to see if the specified character is uppercase (A–Z).

 

Parameters:

Character—The character to be tested.

 

Returns:

A non-zero if the character is uppercase or a zero if it is not.

Example:

if (isupper(chChar))

 

 

putc(chChar);

 

Note:

To test for a lowercase character, use islower().

 

isxdigit()

 

 

Header:

ctype.h

 

Syntax:

int isxdigit(int Character);

 

Description:

Tests to see if the specified character is a valid hexadecimal digit

 

character (a–f, A–F, and 0–9).

 

Parameters:

Character—The character to be tested.

 

Returns:

A non-zero if the character is a valid hexadecimal digit or a zero if

 

it is not.

 

Example:

 

 

 

if (isxdigit(chChar))

 

 

putc(chChar);

 

Note:

To test for a decimal-only digit, use isdigit().

 

labs()

Header:

math.h & stdlib.h

Syntax: long labs(long lValue);

Description: Returns the absolute value of lValue.

551

Part IV • Documenting the Differences

Parameters:

lValue—The value for which absolute value is desired.

Returns:

Absolute value of lValue.

Example:

 

 

long lReturned;

 

lReturned = labs(-234556);

Note:

Also see abs().

ldexp()

Header:

math.h

Syntax:

double ldexp(double lValue, int nPower);

Description:

Multiplies a floating point value by two raised to nPower(lValue

 

* 2nPower).

Parameters:

lValue—Value to multiply.

 

nPower—Power to raise two by.

Returns:

(lValue * (2 * nPower))

Example:

 

 

if (ldexp(.785398, 2) == 3.14259)

 

printf(“It works!\n”);

Note:

See frexp().

ldiv()

Header:

stdlib.h

Syntax:

ldiv_t ldiv(long numerator, long

 

denominator);

Description:

Returns both the quotient and remainder from the division of

 

numerator by denominator.

Parameters:

numerator—Long integer value to be divided.

 

denominator—Long integer value to divide by.

552

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

Returns:

Structure ldiv_t containing the result of the division.

C C

 

Example:

ldiv_t

DivResult;

 

 

DivResult = ldiv(100, 3);

 

Note:

Also see div().

 

localeconv()

 

 

 

Header:

locale.h

 

Syntax:

struct lconv * localeconv(void);

 

Description:

Returns the structure type lconv filled in with appropriate values

 

for the location.

 

Parameters:

None.

 

 

Returns:

Pointer to an lconv structure.

 

Example:

lconv

OurConversions;

 

 

OurConversions = (lconv *)localeconv();

 

Note:

See setlocale() for more information.

 

localtime()

 

 

 

Header:

time.h

 

 

Syntax:

struct tm * localtime(const time_t *

 

 

 

TimeValue);

 

Description:

Breaks down TimeValue, and places the result into the tm

 

structure.

 

Parameters:

TimeValue—Pointer to a time_t variable.

 

Returns:

A pointer to a returned structure of type tm.

 

Example:

tm

TimeStruct;

 

 

time_t

OurTime;

 

OurTime = time(NULL);

TimeStruct = (tm *)localtime(&OurTime);

553

Part IV • Documenting the Differences

Note: Remember to consider the effects of different time zones and daylight savings time.

log()

Header:

math.h

Syntax:

double log(double dValue);

Description:

Computes the natural logarithm (base e) of dValue.

Parameters:

dValue—Value to compute the natural logarithm of.

Returns:

Logarithm of dValue.

Example:

printf(“Log of 3.14159 is %f”,

 

log(3.14159));

 

/* Log of 3.14159 is 1.144729 */

Note:

See log10().

log10()

Header:

math.h

Syntax:

double log10(double dValue);

Description:

Computes the logarithm (base 10) of dValue.

Parameters:

dValue—Value to compute the logarithm of.

Returns:

Logarithm of dValue.

Example:

printf(“Log10 of 3.14159 is %f”,

 

log10(3.14159));

 

/* Log10 of 3.14159 is 0.49715 */

Note:

See log().

longjmp()

Header: setjmp.h

554

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

Syntax:

void longjmp(jmp_buf jumpbuffer, int

C C

 

 

nReturnCode);

 

Description:

Restores the environment to what was saved in jumpbuffer by

 

setjmp(), which causes execution to continue from the call to

 

setjmp(), with setjmp() returning nReturnCode.

 

Parameters:

jumpbuffer—Buffer of type jmp_buf initialized by setjmp().

 

nReturnCode—Value that setjmp() returns when longjmp() is

 

called.

 

Returns:

longjmp() does not return, execution continues with setjmp().

Example:

See Figure 14.1 for an example.

 

Note:

See setjmp().

 

As Figure 14.1 shows, the error handler usually uses longjmp() to get past a part of the program that is causing an error. Since this error handler doesn’t have any access to the failing function’s variables, it cannot make any changes or set any flags.

Figure 14.1. Program flow using setjmp() and longjmp().

555