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

Advanced C 1992

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

Part IV • Documenting the Differences

qsort()

Header:

search.h & stdlib.h

Syntax:

void qsort(void * Array, size_t

 

NumberElements, size_t

 

ElementSize, int ( *compare)

 

(const void *, const void *));

Description:

Sorts Array using a quicksort method.

Parameters:

Array—Pointer to an array to be sorted (can be an array of any

 

type).

 

NumberElements—Number of elements in Array to be sorted.

 

ElementSize—Size of each element in Array.

 

Compare—Pointer to a function to do compare of array elements.

Returns:

No return value.

Example:

See Chapter 10, “Data Management: Sorts, Lists, and Indexes.”

Note:

With a creative Compare function, qsort() can do any sort

 

imaginable.

raise()

Header:

signal.h

Syntax:

int raise(int nException);

Description:

Simulates occurrence of an error condition.

Parameters:

nException—The error condition that is to be signaled.

Returns:

Zero if successful, otherwise a non-zero value.

Example:

raise(SIGINT);

Note:

This function is most useful when you have installed your own

 

exception handler. See signal().

566

 

 

ANSI C’s Library Functions

C C C

 

 

 

14C

 

 

 

C C C

rand()

 

 

C C

 

 

 

Header:

stdlib.h

 

Syntax:

int rand(void);

 

Description:

Returns a pseudorandom number in the range of zero to RAND_MAX

 

(usually 32767).

 

Parameters:

None

 

Returns:

Random number.

 

Example:

int

nRandom = rand();

 

 

/* nRandom will be a random number */

 

Note:

Don’t forget to seed the random number using srand().

 

realloc()

 

 

 

Header:

malloc.h & stdlib.h

 

Syntax:

void * realloc(void * pBuffer, size_t

 

 

 

nNewSize);

 

Description:

Changes the size of the buffer pointed to by pBuffer.

 

Parameters:

pBuffer—Pointer to an allocated buffer.

 

 

nNewSize—New size for the buffer (either smaller or larger).

Returns:

Pointer to a new buffer, with pBuffer’s contents copied to it or

 

NULL if a new buffer could not be allocated.

 

Example:

char

* pBuffer = malloc(sizeof(char) * 100);

 

 

 

strcpy(pBuffer,

 

 

 

“Now is the time for all good men”);

 

 

/* now shrink it... */

 

 

 

pBuffer = realloc

 

 

(pBuffer, strlen(pBuffer) + 1);

 

 

/* This example shrinks the buffer to fit the contents

 

*/

 

 

Note:

Always save the pointer in case the buffer can’t be resized. Never

 

refer to the old pointer after this function successfully returns.

567

Part IV • Documenting the Differences

remove()

Header:

io.h & stdio.h

Syntax:

int remove(const char * szFileName);

Description:

Deletes the file with name that is pointed to by szFileName.

Parameters:

szFileName—Pointer to a character string containing the name

 

of an existing file.

Returns:

Zero if successful, otherwise a non-zero value.

Example:

remove(“test.dat”);

Note:

Be careful to not delete a file that is currently opened because the

 

results may be unpredictable.

rename()

Header:

io.h & stdio.h

Syntax:

int rename(const char * szOldName, const

 

char * szNewName);

Description:

Renames files.

Parameters:

szOldName—Pointer to a string that contains the old filename.

 

szNewName—Pointer to a string containing the new filename.

Returns:

Zero if successful, otherwise a nonzero value.

Example:

rename(“OldData.Dat”, “NewData.Dat”);

Note:

Very useful under PC DOS because it allows renaming a file to a

 

different directory.

rewind()

Header: stdio.h

Syntax: void rewind(FILE * OpenFile);

568

 

ANSI C’s Library Functions

C C C

 

 

14C

 

 

C C C

Description:

 

C C

Resets the file pointer for OpenFile to the beginning of the file.

Parameters:

OpenFile—Pointer to an opened file.

 

Returns:

No return value.

 

Example:

rewind(OpenFile);

 

Note:

Much the same as calling fseek(OpenFile, 0, SEEK_SET).

 

scanf()

 

 

Header:

stdio.h

 

Syntax:

int scanf(const char * szFormat, ...);

 

Description:

Reads from stdin formatted input.

 

Parameters:

szFormat—Pointer to a string containing format codes.

 

Returns:

Number of items that were scanned and stored or EOF if the end

 

of the file was encountered.

 

Example:

int i;

 

 

scanf(“%d”, &i);

 

 

/* i will be whatever (numeric) value

 

 

entered */

 

Note:

See the section on scanf() format codes at the end of this chapter.

setbuf()

 

 

Header:

stdio.h

 

Syntax:

void setbuf(FILE * OpenFile, char *

 

 

pBuffer);

 

Description:

Sets a buffer for the file OpenFile.

 

Parameters:

OpenFile—Pointer to an opened file.

 

 

pBuffer—Pointer to a buffer of at least BUFSIZ bytes.

 

Returns:

No return value.

 

569

Part IV • Documenting the Differences

Example:

char

szBuffer[123];

 

char * pBuffer = malloc(BUFSIZ);

 

 

setbuf(stdin, pBuffer);

 

 

printf(“enter a string\n”);

 

 

gets(szBuffer);

 

 

printf(“enter a second string\n”);

 

 

gets(szBuffer);

 

 

printf(“‘%s’\n”, pBuffer);

Note:

Be sure the buffer is large enough (use BUFSIZ).

setjmp()

Header:

setjmp.h

Syntax:

int setjmp(jmp_buf jumpbuffer);

Description:

Saves the environment to jumpbuffer, which then can be used by

 

longjmp() to return to the point saved.

Parameters:

jumpbuffer—a buffer of type jmp_buf.

Returns:

setjmp() returns zero when being initialized or the return code

 

specified by longjmp(), which will not be zero.

Example:

See Figure 14.2 for an example.

Note:

See longjmp().

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

570

ANSI C’s Library Function

C C C

 

14C

 

C C C

 

C C

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

setlocale()

Header: locale.h

Syntax: char * setlocale(int Category, const char *

Locale);

Description: Sets the category for the locale.

Parameters: Category—Category to set (see Table 14.4).

Locale—The locale to set.

571

Part IV • Documenting the Differences

Returns: String indicating the current locale.

Example: setlocale(LC_ALL, “C”);

Note: Because most compilers and operating systems support only the “C” locale, this function doesn’t have an effect. As new locales are added, this function will be more useful. Check the documentation supplied with your compiler for other information.

Table 14.4. Locale categories.

Category Description

LC_ALL

LC_MONETARY

LC_COLLATE

LC_NUMERIC

LC_CTYPE

C_TIME

Entire environment. Money format. Collate sequence. Number format. Character handling. Time-related items

setvbuf()

Header: stdio.h

Syntax: int setvbuf(FILE * OpenFile, char * pBuffer, int nMode, size_t nSize);

Description: Sets a buffer for the file OpenFile.

Parameters: OpenFile—Pointer to an opened file. pBuffer—Pointer to a buffer.

nMode—Mode for the file buffer (see Table 14.5). nSize—Buffer.

Returns: Zero if successful, otherwise a nonzero value.

572

 

 

 

ANSI C’s Library Function

C C C

 

 

 

 

14C

 

 

 

 

C C C

Example:

char

szBuffer[123];

C C

 

 

 

 

char * pBuffer = malloc(BUFSIZ * 2);

 

 

 

 

 

setvbuf(stdin, pBuffer, _IOFBF, BUFSIZ *

 

 

 

 

 

2);

 

 

 

 

 

printf(“enter a string\n”);

 

 

 

 

 

gets(szBuffer);

 

 

 

 

 

printf(“enter a second string\n”);

 

 

 

 

 

gets(szBuffer);

 

 

 

 

 

printf(“‘%s’\n”, pBuffer);

 

 

Note:

Be sure the buffer is large enough to be effective. Table 14.5 shows

 

 

the allowable modes.

 

 

 

Table 14.5. Function setvbuf()’s modes.

 

 

 

 

 

 

 

 

 

Mode

 

Description

 

 

 

 

 

 

 

 

IOFBF

IOLBF

The input and output will be fully buffered.

The output will be line buffered (buffer is flushed when a newline is encountered or the buffer is full).

IONBF

No buffering is performed (All parameters except

 

OpenFile and nMode are ignored).

 

 

signal()

Header: signal.h

Syntax: void (* signal(int nSignal,

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

573

Part IV • Documenting the Differences

Description: Tells the system to call function whenever the error condition specified by nSignal is raised by either an error condition or the raise() function.

Parameters: nSignal—The error condition to be modified.

function—The function to be called when the error condition is raised. See Table 14.6 for other values for function.

Returns: SIG_ERR if an error occurs, otherwise the previous signal handler.

If function is one of the defined constants shown in Table 14.6, then the action described will be taken. Notice that after the error condition occurs, signal() must again be called because the system first makes a call to signal(nSignal, SIG_DFL).

Table 14.6. Signal predefined actions.

Defined value

Description

SIG_DFL

SIG_IGN

SIG_ACK

The default action will occur.

The condition will be ignored.

Used in some systems to tell the operating system the handler is ready to receive the next error signal.

sin()

Header: math.h

Syntax: double sin(double dValue);

Description: Computes the sine of dValue.

Parameters: dValue—Value to compute the sine of.

Returns:

The sine of dValue.

Example: dValueSine=sin(.5);

/* dValueSine will be 0.4794 */

574

ANSI C’s Library Function

C C C

 

14C

 

C C C

 

C C

Note: See acos(), asin(), and cos().

sinh()

Header: math.h

Syntax: double sinh(double dValue);

Description: Computes the hyperbolic sine of dValue.

Parameters: dValue—Value to compute the hyperbolic sine of.

Returns: The hyperbolic sine of dValue.

Example: dValueSine = sinh(.5);/* dValueSine will be 0.5210 */

Note: See acos(), asin(), and cos().

sprintf()

Header: stdio.h

Syntax: int sprintf(char * pBuffer,

const char * szFormat, ...);

Description: Prints, to the buffer pointed to by pBuffer, formatted output as defined by szFormat.

Parameters: pBuffer—Pointer to a destination buffer.

szFormat—A format descriptor string.

Returns: Number of characters written. If negative, then an error occurred.

Example: char szBuffer[100];

sprintf(szBuffer,

“The number one is %d\n”, 1);

Note: Be sure the destination buffer is large enough. See the section on format codes at the end of this chapter.

575