facebook like button

06 April, 2011

program 28: getting ASCII code of any character

The need:
  This program was made to get ASCII code for any given character. 

The code: 
--------------------------------------------
#include<stdio.h>
#include<math.h>
int main()
{
    char i;
    printf("Enter the character to get its ASCII code.\n");
    scanf("%c",&i);
    printf("\nASCII code of `%c` is %d\n\n",i,i);
    printf("\n");
    return 0;
}
-----------------------------------------

The approach:     
     Probably you are aware of the fact that in computer's language everything is combination of 0 and 1. You know that integers can be converted into binary numbers and stored in computer. But question comes how characters are converted and stored? Answer is: there is a standard encoding scheme called ASCII which assigns an integer to each character and then the character is converted into 0 and 1's and stored in memory of computer. Worth notice point is this: each character is stored as integer in memory it is the printing style which differentiate between character and integer. So to print ASCII code of any character just print the same variable which is storing that character with %d which has been done here.

Remarks:
    One more point I wanted to show that if we include header file which is not at all required then there is no harm provided your compiler has that header file. Note that I have included math.h header file in this program while there is no function in the program that requires that header file.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply