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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
int main(int argc, char* argv[]) {
// int n = 111; // The RHS (right-hand side) is an integer literal in base 10.
// The variable `n` is just a quantity. It has no number base.
// int n = 0x60 + 0x0f; // Hexadecimal: digits are 0 to f.
// int n = 0x60 + 15; // 0x60 is hexadecimal 15 is decimal
int n = 0x60 + 017; // 0x60 is hexadecimal 017 is octal
printf("n is %d (written in base 10)\n", n); // d in %d stands for decimal
printf("n is %o (written in base 8)\n", n); // o in %o stands for octal
printf("n is %x (written in base 16)\n", n); // x in %x stands for heXadecimal
// TAKEAWAY: A quantity does not have a number base.
// Number base is only relevant when a number is written in digits.
// ∙ In our code, we can write integer literals using a number base 8, 10, or 16.
// ∙ printf(…) can express a quantity in base 8, 10, or 16.
// ∙ You can write code to express a quantity in any number base you like.
// ∙ See HW02.
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2021 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.