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

Advanced C 1992

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

Part IV • Documenting the Differences

atof()

Header:

math.h & stdlib.h

Syntax:

double atof(const char * szString);

Description:

Converts characters to a double value.

Parameters:

szString—Pointer to a string containing the character represen-

 

tation of the double number.

Returns:

A double value.

Example:

dValue = atof(“1234.56”);

 

/* dValue will be 1234.56 */

Note:

Conversion continues until the first invalid character is reached.

 

You can test errno to determine any errors that occurred.

atoi()

Header:

stdlib.h

Syntax:

int atoi(const char * szString);

Description:

Converts characters to a short integer value.

Parameters:

szString—Pointer to a string containing the character represen-

 

tation of the integer.

Returns:

A short integer.

Example:

nValue = atoi(“1234”);

 

/* nValue will be 1234 */

Note:

Conversion continues until the first invalid character is reached.

 

You can test errno to determine any errors that occurred.

atol()

Header: stdlib.h

Syntax: long atol(const char * szString);

526

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

Description:

Converts characters to a long (32-bit) integer.

C C

 

Parameters:

szString—Pointer to a string containing the character

 

representation of the integer.

 

Returns:

A long integer.

 

Example:

lValue = atol(“12345678”);

 

 

/* lValue will be 12345678 */

 

Note:

Conversion continues until the first invalid character is reached.

 

You can test errno to determine any errors that occurred.

 

bsearch()

 

 

Header:

search.h & stdlib.h

 

Syntax:

void * bsearch((void *key, const void *base,

 

 

size_t num, size_t width,

 

 

int (*compare)(const void *elem1,

 

 

const void

 

 

*elem2));

 

Description:

Searches a sorted list for the given key value.

 

Parameters:

key—Pointer to the key value to search for.

 

 

base—Pointer to the array to search.

 

 

num—Number of elements in the array being searched.

 

 

width—Size of an element in the array being searched.

 

 

compare—Pointer to a function that is doing the compare.

 

Returns:

Either a pointer to a matching element or NULL if the key was not

 

found in the list.

 

Example:

/* See Chapter 10, “Data Management: Sorts, Lists,

 

 

and Indexes.”*/

 

Note:

The array to be searched must be sorted; if it is not, the results are

 

undefined.

 

527

Part IV • Documenting the Differences

calloc()

Header:

malloc.h & stdlib.h

Syntax:

void * calloc(size_t nCount, size_t nSize);

Description:

Allocates an array.

Parameters:

nCount—Number of elements.

 

nSize—Size of each array element.

Returns:

Either a pointer to the array or NULL if the memory couldn’t be

 

allocated.

Example:

int

*pOurArray;

pOurArray = calloc(100, sizeof(int));

Note: The calloc() function initializes the memory to zero.

ceil()

Header:

math.h

Syntax:

double ceil(double dValue);

Description:

Returns the smallest integer value not less than dValue.

Parameters:

dValue—Number for which the ceiling value is desired.

Returns:

The integer ceiling value, converted to a double.

Example:

dCeil = ceil(-2.2);

 

/* dCeil will be -2.0 */

Note:

See floor().

clearerr()

Header:

stdio.h

Syntax:

void clearerr(FILE * filepointer);

Description:

Clears an existing end of file or other error condition for the

 

given file.

528

ANSI C’s Library Functions

Parameters:

filepointer—Pointer to a stream file.

Returns:

No return value.

Example:

if (ferror(OpenFile))

 

{

 

clearerr(OpenFile);

 

}

Note:

See ferror() and fopen().

clock()

C C C

C14C C

C C C

Header:

time.h

Syntax:

clock_t clock(void);

Description:

Provides the number of clock ticks (amount of CPU time) used by

 

the program since it started.

Parameters:

None.

Returns:

CPU time.

Example:

printf(“CPU time is %d\n”, clock() /

 

CLOCKS_PER_SEC);

Note:

The time returned is not elapsed time, but actual CPU time. In

 

multitasking systems, returned time varies greatly from elapsed

 

time. You convert this time returned by dividing it with the macro

 

CLOCKS_PER_SEC which is defined in time.h.

cos()

Header:

math.h

Syntax:

double cos(double dValue);

Description:

Returns the cosine of dValue (in radians).

Parameters:

dValue—Value to compute the cosine of.

Returns:

Cosine of dValue.

529

Part IV • Documenting the Differences

Example:

dReturned = cos(0.5)

 

/* dReturned will be 0.877583 */

Note:

When dValue is a large value, the result may not be significant.

cosh()

Header:

math.h

Syntax:

double cosh(double dValue);

Description:

Returns the hyperbolic cosine of dValue (in radians).

Parameters:

dValue—Value to compute the hyperbolic cosine of.

Returns:

Hyperbolic cosine of dValue.

Example:

dReturned = cosh(0.5)

 

/* dReturned will be 1.1276 */

Note:

When dValue is a large value, the result may not be significant.

ctime()

Header:

time.h

 

Syntax:

char * ctime(const time_t * TimeBuffer);

Description:

Converts the time pointed to by TimeBuffer into a printable

 

format.

 

Parameters:

TimeBuffer—Pointer to a data object of type time_t, properly

 

initialized (perhaps by using time()).

Returns:

Pointer to a character string, which is formatted as the example:

 

Fri Jun

26 15:17:00 1992\n\0

Example:

time_t

OurTime = time(NULL);

 

printf(ctime(&OurTime);

Note:

This function is equal tocallingasctime(localtime(TimeBuffer)).

530

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

difftime()

 

C C

 

 

Header:

time.h

 

Syntax:

double difftime(time_t starttime, time_t

 

 

endtime);

 

Description:

Computes and returns the difference between starttime and

 

endtime (in seconds).

 

Parameters:

startime—Time interval start.

 

 

endtime—Time interval end.

 

Returns:

Double time difference, in seconds.

 

Example:

time_t StartTime = time(NULL);

 

 

time_t EndTime;

 

 

char szBuffer[100];

 

 

printf(“Wait a few seconds, and press

 

 

return\n”);

 

 

gets(szBuffer);

 

 

EndTime = time(NULL);

 

 

printf(“You waited %f seconds\n”,

 

 

difftime(EndTime, StartTime);

 

Note:

Don’t forget that the difference is in seconds.

 

div()

 

 

Header:

stdlib.h

 

Syntax:

div_t div(int numerator, int denominator);

 

Description:

Returns both the quotient and remainder from the division of

 

numerator by denominator.

 

Parameters:

numerator—Integer value to be divided.

 

 

denominator—Integer value to divide by.

 

Returns:

Structure div_t containing the result of the division.

 

531

Part IV • Documenting the Differences

Example:

div_t

DivResult;

 

DivResult = div(100, 3);

Note:

Also see ldiv().

exit()

Header:

process.h & stdlib.h

Syntax:

void exit(int nExitCode);

Description:

Causes the program to end.

Parameters:

nExitCode—An integer passed back to the parent process.

Returns:

Does not return.

Example:

exit(0);

Note:

On MS-DOS systems, only the low order byte of nExitCode is

 

available.

exp()

Header:

math.h

Syntax:

double exp(double dValue);

Description:

Returns the exponential value of dValue, such that exp(x)=ex.

Parameters:

dValue—Value whose exponential value is desired.

Returns:

Exponential value of dValue.

Example:

double dExp;

 

dExp = exp(.5);

 

/* dExp will be 1.6487 */

Note:

An ERANGE error occurs if dValue is too large.

532

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

fabs()

 

C C

 

 

Header:

math.h

 

Syntax:

double fabs(double dValue);

 

Description:

Returns the absolute value of dValue.

 

Parameters:

dValue—Double for which absolute value is desired.

 

Returns:

The absolute value of dValue.

 

Example:

double dAbs = fabs(-0.2);

 

 

/* dAbs will be 0.2 */

 

Note:

Also see abs().

 

fclose()

 

 

Header:

stdio.h

 

Syntax:

int fclose(FILE * OpenFile);

 

Description:

Closes the open stream file pointed to by OpenFile.

 

Parameters:

OpenFile—Pointer to a FILE structure.

 

Returns:

Zero if the function is successful.

 

Example:

fclose(OpenFile);

 

Note:

If the function fails, then errno contains the error code.

 

feof()

 

 

Header:

stdio.h

 

Syntax:

int feof(FILE * OpenFile);

 

Description:

Tests for an end of file condition on OpenFile.

 

Parameters:

OpenFile—Pointer to a FILE structure for an opened file.

 

Returns:

A non-zero if the file is at end of file.

 

533

Part IV • Documenting the Differences

Example:

int

nEndOfFile = feof(OpenFile);

 

 

/* nEndOfFile is zero if not end of

file */

Note:

Also see clearerr() for clearing the end of file condition.

ferror()

Header:

stdio.h

 

Syntax:

int ferror(FILE * OpenFile);

Description:

Tests for any error conditions for the stream file OpenFile.

Parameters:

OpenFile—Pointer to a FILE structure for an opened file.

Returns:

A non-zero if there is an error associated with OpenFile.

Example:

int

nError = ferror(OpenFile);

 

/* nError will be zero if no errors. */

Note:

Also see clearerr() for clearing errors.

fflush()

Header:

stdio.h

 

Syntax:

int fflush(FILE * OpenFile);

Description:

For output files, fflush() writes any unwritten characters in the

 

file’s buffer to the file. For input files, fflush() will undo the last

 

ungetc(). If OpenFile is NULL, then all open files are flushed.

Parameters:

OpenFile—Pointer to a FILE structure for an opened file or NULL

 

for all files.

Returns:

A non-zero if an error is associated with OpenFile.

Example:

int

nError = fflush(OpenFile);

 

/* nError is zero if no errors in flushing.

 

*/

 

Note:

Also see clearerr() for clearing errors. Frequently flushing

 

output files helps prevent data loss if the computer crashes.

534

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

fgetc()

 

 

C C

 

 

 

Header:

stdio.h

 

 

Syntax:

int fgetc(FILE * OpenFile);

 

Description:

Gets the next character from OpenFile.

 

Parameters:

OpenFile—Pointer to a FILE structure for an opened input file.

Returns:

The next character from OpenFile, or EOF if either an error occurs

 

or the end-of-file is reached.

 

Example:

char

chChar = (char)fgetc(OpenFile);

 

 

/* chChar contains the next character

 

 

from the file. */

 

Note:

Also see clearerr() for clearing errors. Getting single characters

 

at a time can be inefficient; if possible, use fgets() to get an entire

 

line at a time.

 

fgetpos()

 

 

 

Header:

stdio.h

 

 

Syntax:

int fgetpos(FILE * OpenFile, fpos_t *

 

 

 

Position);

 

Description:

Saves the current position of OpenFile in the variable pointed to

 

by Position.

 

Parameters:

OpenFile—Pointer to a FILE structure for an opened input file.

 

Position—Pointer to a variable of type fpos_t.

 

Returns:

A non-zero if there is an error.

 

Example:

fpos_t

Position;

 

 

fgetpos(OpenFile, &Position);

 

 

/* Position will contain the file current

 

 

position. */

 

Note:

Usually, you use fseek() to reset the file to the point indicated by

 

Position.

 

535