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 | // IMPLEMENTATION FILE
//
// Okay to copy/adapt anything in this file for Spring 2021. You are
// responsible for ensuring that it is correct.
#include <stdio.h>
#include <stdlib.h>
void print_integer(int n, int radix, char* prefix) {
int i = 0;
// while(prefix[i] == '\0') { // BUG!!!!
while(prefix[i] != '\0') { // stop if the current character is the null terminator
fputc(prefix[i], stdout);
i += 1;
}
// REMEMBER: In memory, a string (e.g., "Zebra + Panda = ") is stored as
// an array of characters (char) followed by a null terminator ('\0') which
// tells C functions that you are at the end, since there is no other way to
// know the length of a string.
//
// FACT: Every for loop can be written as a while loop.
if(n <= 9) {
fputc('0' + n, stdout); // GOOD
}
else {
// fputc('0' + n + 49, stdout); // BAD
fputc('a' + (n - 10), stdout); // GOOD
}
// fputc(48 + n, stdout); // BAD!!!! unclear. violates code quality standard
// WARNING: DO NOT USE AN INTEGER LITERAL IF YOU ARE REFERRING TO A CHARACTER.
// fputc(48, stdout); // BAD!!!!!!!!!
}
// NO RETURN STATEMENT in a function that returns void.
|
© Copyright 2021 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.