Previous Table of Contents Next


atof, strtod

double atof(const char *s)
double strtod(const char *s, char **endp)

These functions convert numeric strings to floating-point numbers.

The more general function is strtod. It skips over any leading whitespace, honors a leading sign character (‘-’ or ‘+’), and then converts a string representing a floating-point number. The format accepted is the same as for C’s floating-point constants, and can contain a decimal-point character and/or the letter e or E indicating an exponent. If endp is non-null, strtod stores into the pointed-to pointer *endp a pointer to the first character in s that it did not consume. The caller can use the returned pointer just as for strtol, et al. (see section 3.10.4.2.): to discriminate between zero returns due to errors or real zeros and to detect trailing garbage. In case of overflow, the return value is plus or minus HUGE_VAL, and the global errno is set to ERANGE.

atof(s) is equivalent to strtod(s, NULL), except that the result on overflow is undefined.

3.10.4.3. Pseudo-Random Number Generation

rand

int rand(void)

rand returns a random integer in the range 0 to RAND_MAX, inclusive, where RAND_MAX is a constant defined in <stdlib.h>. One way of obtaining random integers in a reduced range is to compute

rand() / (RAND_MAX / n + 1)

which returns integers less than n, that is, from 0 to n-1, inclusive. (This method gives good results as long as n is much less than RAND_MAX. Note, however, that RAND_MAX might be as small as 32,767.)

srand

void srand(unsigned int seed)

srand seeds the random number generator used by rand with the value seed. By default, a C program starts out as if the equivalent of srand(1) had been performed; that is, the sequence returned by repeated calls to rand will be the same for each run of the program. To achieve more truly random behavior, a program can call srand with a random seed value (perhaps derived from the time of day or a process ID). A single call to srand usually suffices for each run of the program; one simple invocation is

srand(time((time_t *)NULL));

(See section 3.10.6.1 for a discussion of the time function.)

3.10.4.4. Sorting and Searching

qsort

void qsort(void *a, size_t n, size_t elsize, int (*cmpfunc)())

qsort sorts the array a, according to an ordering relation defined by the function pointed to by cmpfunc. The array is of n elements, each of size elsize. To determine whether two elements are in order, qsort calls *cmpfunc, passing pointers to the two elements. The comparison function must therefore accept two generic void * pointers and convert them to pointers to the actual data type being sorted before comparing them. The comparison function must be declared as

int name(const void *p1, const void *p2)

and must return an integer less than, equal to, or greater than 0 according to whether the element pointed to by p1 is less than, equal to, or greater than that pointed to by p2, respectively. (Strictly speaking, then, qsort’s fourth parameter is declared as int (*cmpfunc)(const void *, const void *).)

For example, to sort an array of strings represented by char * pointers, the following comparison function could be used:

int pstrcmp(const void *p1, const void *p2)
{
       const char *s1 = *(const char **)p1;
       const char *s2 = *(const char **)p2;
       return strcmp(s1, s2);
}

strcmp cannot be used directly as a qsort comparison function in this case because it compares pointers to char, whereas the qsort comparison function is passed pointers to the elements being compared, or in this case, pointers to pointers to char.

bsearch

bsearch(const void *key, const void *a, size_t n, size_t elsize,
                int (*cmpfunc)())

bsearch performs a binary search on the sorted array a, searching for an element matching the one pointed to by key. The array is of n elements, each of size elsize (as is the element pointed to by key, of course). The search is directed by a caller-supplied function pointed to by cmpfunc. cmpfunc’s specification is the same as described for qsort. (In fact, qsort with the same cmpfunc is often used to sort the array prior to calling bsearch.)


Previous Table of Contents Next