Βοηθητικές συναρτήσεις
Επικεφαλίδα
#include <stdlib.h>
Περιλαμβάνει
Συναρτήσεις μετατροπής αριθμών,
χρήσης δυναμικής μνήμης
(βλ. δυναμική διάθεση μνήμης),
λειτουργίας στο περιβάλλον του λειτουργικού συστήματος και
χρήσης ταξινομημένων πινάκων.
Βασικές συναρτήσεις
- atof
 - Μετατρέπει συμβολοσειρά σε double
 - atol
 - Μετατρέπει συμβολοσειρά σε long
 - atoi
 - Μετατρέπει συμβολοσειρά σε int
 - abs
 - Επιστρέφει την απόλυτη τιμή ενός int
 - max
 - Επιστρέφει το μέγιστο δύο αριθμών
 - min
 - Επιστρέφει τον ελάχιστο δύο αριθμών
 - rand
 - Επιστρέφει έναν ψευδοτυχαίο αριθμό 0 - RAND_MAX
 - srand
 - Καθορίζει τη βάση εκκίνησης της rand
 - malloc
 - Δεσμεύει και επιστρέφει δυναμική μνήμη
 - free
 - Ελευθερώνει δυναμική μνήμη
 - realloc
 - Αλλάζει το μέγεθος ενός τμήματος δυναμικής μνήμης
 - exit
 - Τερματίζει το πρόγραμμα
 - system
 - Εκτελεί εντολή του λειτουργικού συστήματος
 - getenv
 - Επιστρέφει την τιμή μιας μεταβλητής του λειτουργικού συστήματος
 - qsort
 - Ταξινομεί έναν πίνακα
 - bsearch
 - Δυαδική αναζήτηση σε ταξινομημένο πίνακα
 
Παράδειγμα
Το παρακάτω παράδειγμα γεμίζει έναν πίνακα με ψευδοτυχαίους αριθμούς,
τον ταξινομεί και επιτρέπει στο χρήστη να ψάξει για έναν αριθμό
μέσα στον πίνακα.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/*
 * Qsort/bsearch comparison function
 */
int
compare(const int *a, const int *b)
{
        return (*a - *b);
}
/*
 * Print an integer table contents using a given label
 */
void
dump_int_table(char *label, int *table, int nelem)
{
        int i;
        printf("%s\n", label);
        for (i = 0; i < nelem; i++)
                printf("(%d, %d) ", i, table[i]);
        putchar('\n');
}
main()
{
        int *tbl;                       /* Integer table */
        char buff[10];                  /* User input */
        int n, i;                       /* User number, counter */
        int *pos;                       /* Found position */
        const int TABLE_SIZE = 64;
        /* Randomize the random number generator using current time */
        srand(time(NULL));
        /* Allocate table memory */
        tbl = (int *)malloc(TABLE_SIZE * sizeof(int));
        /* Fill table with random numbers (0 - 2 * TABLE_SIZE) */
        for (i = 0; i < TABLE_SIZE; i++)
                tbl[i] = rand() % (TABLE_SIZE * 2);
        dump_int_table("Table with random numbers", tbl, TABLE_SIZE);
        /* Sort the table contents */
        qsort(tbl, TABLE_SIZE, sizeof(int), compare);
        dump_int_table("Sorted table with random numbers", tbl, TABLE_SIZE);
        /* Read and convert a number from the user (without using scanf) */
        printf("Search for which number? (0-%d):", 2 * TABLE_SIZE);
        fgets(buff, sizeof(buff), stdin);
        n = atoi(buff);
        /* Search for the number and report result */
        pos = bsearch(&n, tbl, TABLE_SIZE, sizeof(int), compare);
        if (pos)
                printf("The number %d is located at table position %d\n",
                        n, pos - tbl);
        else
                printf("The number %d is not in the table\n");
        /* Free memory and exit - not strictly needed */
        free(tbl);
        exit(0);
}