1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// okay to copy/adapt
#include "print_integer.h"
#include <stdio.h>
#include <assert.h>

// helper function
void _print_digit(int digit, int radix) {
    // TODO: handle hexadecimal/bases higher than 10
    // remove assert once handled
    assert(radix <= 10);
    fputc('0' + (digit % radix), stdout);
}

void print_integer(int n, int radix, char* prefix) {
    assert(radix >= 2 && radix <= 36);

    if (n > radix) {
        _print_digit(n / radix, radix);
    }
    _print_digit(n, radix);
}

© Copyright 2024 Alexander J. Quinn & David Burnett         This content is protected and may not be shared, uploaded, or distributed.