Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
Library Functions
 2012 Microchip Technology Inc.
DS52053B-page 355
Example
#include <stdio.h>
#include <stdlib.h>
int array[] = {
    567, 23, 456, 1024, 17, 567, 66
};
int 
sortem (const void * p1, const void * p2)
{
    return *(int *)p1 - *(int *)p2;
}
void 
main (void)
{
    register int i;
    qsort(aray, sizeof array/sizeof array[0],
        sizeof array[0], sortem);
    for(i = 0 ; i != sizeof array/sizeof array[0] ; i++)
        printf("%d\t", array[i]);
    putchar(’\n’);
}
Note
The function parameter must be a pointer to a function of type similar to:
int func (const void *, const void *)
For example, it must accept two const void * parameters, and must be prototyped.
RAND 
Synopsis
#include <stdlib.h>
 
int rand (void)
Description
The 
rand()
 function is a pseudo-random number generator. It returns an integer in the 
range 0 to 32767, which changes in a pseudo-random fashion on each call. The algo-
rithm will produce a deterministic sequence if started from the same point. The starting 
point is set using the 
srand()
 call. The example shows use of the 
time()
 function to 
generate a different starting point for the sequence each time.