1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include "clog.h"
#include "print_integer_simple.h"
// INTERNAL HELPER FUNCTIONS
// - name should start with '_'.
// - declare with static qualifier (like below).
// RULE: static function is only accessible within the file where it was defined.
// NOTE: static gets used in different ways in C. We're just talking about one of them.
static char _get_digit_char(int digit_value) {
assert(digit_value >= 0 && digit_value <= 35); // sanity check
if(digit_value <= 9) {
return '0' + digit_value;
}
else {
return 'a' + (digit_value - 10);
}
}
void print_integer_simple(int n, int radix) {
int digit_value = n % radix;
char digit_char = _get_digit_char(digit_value);
fputc(digit_char, stdout);
// TODO: make this work for negative numbers and multi-digit.
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.