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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
union Number {
int as_int;
double as_double;
char as_char;
};
void print_number(union Number n, int which) { // BAD! use better way (TBA)
if(which == 1) { // BAD: do not embed integers as magic numbers
printf("%d\n", n.as_int);
}
else if(which == 2) { // BAD: do not embed integers as magic numbers
printf("%f\n", n.as_double);
}
else if(which == 3) { // BAD: do not embed integers as magic numbers
printf("'%c'\n", n.as_char);
}
}
int main(int argc, char* argv[]) {
union Number n = { .as_double = 3.1415962 };
print_number(n, 2); // BAD: do not embed integers as magic numbers
return EXIT_SUCCESS;
}
// double is like a float but twice the precision
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.