Flylib.com

Books Software

 
 
 

C Programming on the IBM PC (C Programmers Reference Guide Series) - page 332

rand

#include <stdlib.h>int rand(void);

The rand( ) function generates a sequence of pseudorandom numbers . Each time it is called, an integer between zero and RAND_MAX is returned. RAND _MAX will be at least 32,767.

A related function is srand ( ) .


setjmp

#include <setjmp.h>int setjmp(jmp_buf

envbuf

);

The setjmp( ) macro stores the contents of the system stack in the buffer envbuf for later use by longjmp( ) . It uses the header < setjmp.h >, or < csetjmp > in a C++ program.

The setjmp( ) macro returns zero upon invocation. However, longjmp(  ) passes an argument to setjmp( ) , and it is this value (always nonzero) that will appear to be the value of setjmp( ) after a call to longjmp( ) has occurred. See longjmp( ) .

A related function is longjmp( ) .


signal

#include <signal.h>void (*signal(int

signal

, void (*

func

)(int))) (int);

The signal( ) function registers the function pointed to by func as a handler for the signal specified by signal . That is, the function pointed to by func will be called when signal is received by your program. It uses the header < signal.h >, or < csignal > in a C++ program.

The value of func can be the address of a signal handler function or one of the following macros, defined in < signal.h >:

Macro

Meaning

SIG_DFL

Use default signal handling

SIG_IGN

Ignore the signal

If a function address is used, the specified handler will be executed when its signal is received. Check your compiler’s documentation for additional details.

On success, signal( ) returns the address of the previously defined function for the specified signal. On error, SIG_ERR (defined in < signal.h >) is returned.

A related function is raise( ) .


srand

#include <stdlib.h>void srand(unsigned int

seed

);

The srand( ) function sets a starting point for the sequence generated by rand( ) . (The rand( ) function returns pseudorandom numbers .)

srand( ) is often used to allow multiple program runs to use different sequences of pseudorandom numbers by specifying different starting points. Conversely, you can also use srand( ) to generate the same pseudorandom sequence over and over again by calling it with the same seed before starting the sequence.

A related function is rand( ) .


strtod

#include <stdlib.h>double strtod(const char *

start

, char **

end

);

The strtod( ) function converts the string representation of a number stored in the string pointed to by start into a double and returns the result.

In C99, start and end are qualified by restrict .

The strtod( ) function works as follows . First, any whitespace in the string pointed to by start is stripped. Next, each character that comprises the number is read. Any character that cannot be part of a floating-point number will cause this process to stop. This includes whitespace, punctuation (other than periods), and characters other than E or e. Finally, end is set to point to the remainder, if any, of the original string. This means that if strtod( ) is called with “100.00 Pliers”, the value 100.00 will be returned and end will point to the space that precedes “Pliers”.

If overflow occurs, either HUGE_VAL or – HUGE_VAL (indicating positive or negative overflow) is returned, and the global variable errno is set to ERANGE , indicating a range error. If underflow occurs, the function returns zero and the global variable errno is set to ERANGE . If start does not point to a number, no conversion takes place and zero is returned.

Related functions are atof( ) , strtold ( ) , and strtof ( ) .