/* integers.c Jeff Ondich, 16 Jan 2023, originally named integers2.c Modified by Tanya Amert for Fall 2024 How are integers specified? What kinds of values do they have? Compile as usual: gcc -Wall -Werror -g -o integers integers.c Run with no command-line arguments: ./integers */ #include #include // atoi comes from here, and converts a null-terminated // char array (a "C string") into a number void print_usage_statement(const char *program_name); // For each section: uncomment, predict output, try it. int main(int argc, char *argv[]) { int j; if (argc != 2) { print_usage_statement(argv[0]); } // Run exactly one section using a switch statement // (note the break; lines) int section = atoi(argv[1]); switch (section) { case 1: fprintf(stdout, "== SECTION 1: fprintf output specifiers ==\n"); j = 165; fprintf(stdout, "Decimal: %d\n", j); fprintf(stdout, "Hexadecimal (lower case): 0x%x\n", j); fprintf(stdout, "Hexadecimal (upper case): 0x%X\n", j); fprintf(stdout, "Octal: %o\n", j); break; case 2: fprintf(stdout, "== SECTION 2: specifying an integer in binary ==\n"); j = 0b10100101; fprintf(stdout, "Decimal: %d\n", j); fprintf(stdout, "Hexadecimal: 0x%X\n", j); break; case 3: fprintf(stdout, "== SECTION 3: specifying an integer in hexadecimal ==\n"); j = 0xA5; fprintf(stdout, "Decimal: %d\n", j); fprintf(stdout, "Hexadecimal: 0x%X\n", j); break; case 4: fprintf(stdout, "== SECTION 4: what is going on here?!? ==\n"); j = 0254; fprintf(stdout, "Decimal: %d\n", j); fprintf(stdout, "Hexadecimal: 0x%X\n", j); fprintf(stdout, "Octal: %o\n", j); break; case 5: fprintf(stdout, "== SECTION 5: specifying a peculiar hex value ==\n"); j = 0xFFFFFFFE; fprintf(stdout, "Hexadecimal: 0x%X\n", j); fprintf(stdout, "Decimal: %d\n", j); break; case 6: fprintf(stdout, "== SECTION 6: isn't this an infinite loop? ==\n"); j = 1; while (j > 0) { j++; } fprintf(stdout, "We made it! j = %d\n", j); /* What happened? Why does j have the value it has? */ break; default: fprintf(stderr, "You need to specify section_num between 1 and 6, inclusive.\n"); break; } return 0; } // Prints information for how to use this program. void print_usage_statement(const char *program_name) { fprintf(stderr, "Usage: %s section_num\n", program_name); fprintf(stderr, " shows different ways to specify/use integers,\n"); fprintf(stderr, " where section_num is an integer from 1 to 6, inclusive\n"); }