How to write a C Program to Printing ASCII Table with Numbers and corresponding characters in C Programming Language ?
The following program prints the ASCII Table with the numbers from 0 to 255 and their corresponding equivalent characters in ASCII.Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.
- #include<stdio.h>
- main()
- {
- int num;
- printf("Printing ASCII values Table...\n\n");
- num = 1;
- while(num<=255)
- {
- printf("\nValue:%d = ASCII Character:%c", num, num); /*This change has been made as per the comment. Thank you anonymous Blog Viewer ... */
- num++;
- }
- printf("\n\nEND\n");
- }
the program should have a char input declaration .. ie..
int num;
char c;
.
.
.
then, after the while loop
printf("\n Value:%d= ASCII character:%c",num,c);
.. because the character variable in the above printf statement cannnot be "num" as it has been declared as the integer variable at the start of the program.
It says that there's not need for the char variable. printf("%c", int_variable)..will always produce an output which is the ASCII equivalent of the then value of that int_variable.
So, introducing a new char variable would be absolutely unnecessary.
Always keep in mind..the programs that we make always have to be compact. The most compact and the most efficient program bags the prize
You could simplify and use a for loop instead.
- #include
- int main(void)
- {
- printf("Printing ASCII values Table...\n\n");
- for (int i = 1; i <= 255; ++i)
- {
- printf("\nValue:%d = ASCII Character:%c", i, i); /*This change has been made as per the comment. Thank you anonymous Blog Viewer ... */
- }
- printf("\n\nEND\n");
- return 0;
- }
We should define (char)c=num(int); such as in this code.
- #include
- #include
- main()
- {
- int num;
- char c;
- printf("Printing ASCII values Table...\n\n");
- num = 1;
- while(num<=255)
- {
- c=num;
- printf("\nValue:%d = ASCII Character:%c", num, c);
- num++;
- }
- printf("\n\nEND\n");
- getche();
- }
C Program to Printing ASCII Table with Numbers and corresponding characters
- #include
- void main()
- {
- int i;
- char j;
- for (i=0,j=0;i<=255;j++,i++)
- {
- printf("character %c ,Value %d\n", j,i);
- }
- }