225k views
5 votes
(Creation of an ASCII Table) Write a C program that implements that ASCII table given at the end of this document. Your program should first print out the column headings. It should then print out the binary, octal, decimal, hexadecimal, and character versions of the ASCII characters from 32 to 126 (decimal), as indicated in the table. Two vertical columns should be sufficient.

User Ppwater
by
8.6k points

1 Answer

5 votes

Final answer:

A C program can be written to print an ASCII table by iterating from character 32 to 126 and using a custom function to display binary representations, along with standard functions for octal, decimal, hexadecimal, and character display.

Step-by-step explanation:

Creation of an ASCII Table in C

To create a C program that prints the ASCII table from character 32 to 126, you need to iterate over this range and print the corresponding binary, octal, decimal, hexadecimal, and character representations for each value. Below is a segment of code you might include:

#include <stdio.h>

int main() {
printf("Binary\tOctal\tDecimal\tHexadecimal\tCharacter\\");
for(int i = 32; i <= 126; i++) {
printf("%7s\t%o\t%d\t%x\t%c\\", byte_to_binary(i), i, i, i, i);
}
return 0;
}

// Function to convert a byte to its binary representation
const char* byte_to_binary(int x) {
static char b[9];
b[0] = '\0';

for (int z = 128; z > 0; z >>= 1) {
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}

Remember that the byte_to_binary function needs to be implemented for binary conversion, as C does not have a built-in function for this purpose. This sample code includes a simple function for such a conversion.

User Bina
by
8.1k points