Write a C program to print ASCII table.
#include <stdlib.h>
#include <string.h>
/* decimal to binary conversion */
char * binary(int data) {
char *ptr, val[8];
int rem, i = 0, j = 0;
/* dynamically allocate memory to return binary value */
ptr = (char *)malloc(sizeof(char) * 8);
memset(ptr, '0', 8);
/* finding binary value */
while (data) {
rem = data % 2;
val[i++] = rem ? '1' : '0';
data = data / 2;
}
val[i] = '\0';
j = 8 - i;
i--;
while (i > 0) {
ptr[j++] = val[i];
i--;
}
ptr[j] = '\0';
/* returing the found binary value */
return ptr;
}
int main() {
int i, ch;
printf("ASCII Table For Printable Characters:\n");
/* printing the ASCII table for printable characters */
printf(" Decimal Octal Hexa Binary Character\n");
for (i = 33; i < 128; i++) {
printf(" %3d %3o %3x %7s %c\n",
i, i, i, binary(i), i);
if (i % 10 == 0) {
printf("Do you want to continue(0/1):");
scanf("%d", &ch);
if (ch != 1)
break;
}
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
ASCII Table For Printable Characters:
Decimal Octal Hexa Binary Character
33 41 21 0010000 !
34 42 22 0010001 "
35 43 23 0010001 #
36 44 24 0010010 $
37 45 25 0010010 %
38 46 26 0010011 &
39 47 27 0010011 '
40 50 28 0010100 (
Do you want to continue(0/1):1
41 51 29 0010100 )
42 52 2a 0010101 *
43 53 2b 0010101 +
44 54 2c 0010110 ,
45 55 2d 0010110 -
46 56 2e 0010111 .
47 57 2f 0010111 /
48 60 30 0011000 0
49 61 31 0011000 1
50 62 32 0011001 2
Do you want to continue(0/1):0
ASCII Table For Printable Characters:
Decimal Octal Hexa Binary Character
33 41 21 0010000 !
34 42 22 0010001 "
35 43 23 0010001 #
36 44 24 0010010 $
37 45 25 0010010 %
38 46 26 0010011 &
39 47 27 0010011 '
40 50 28 0010100 (
Do you want to continue(0/1):1
41 51 29 0010100 )
42 52 2a 0010101 *
43 53 2b 0010101 +
44 54 2c 0010110 ,
45 55 2d 0010110 -
46 56 2e 0010111 .
47 57 2f 0010111 /
48 60 30 0011000 0
49 61 31 0011000 1
50 62 32 0011001 2
Do you want to continue(0/1):0
No comments:
Post a Comment