Write a Program to print the ASCII value of a character

Posted in

Write a Program to print the ASCII value of a character
vinaykhatri

Vinay Khatri
Last updated on April 20, 2024

    ASCII stands for American Standard Code for Information Interchange. And it represents a positive unique value for every character used in a programming language.

    Problem Statement

    We need to write a program in C, C++, and Python that accepts a character from the user and returns its ASCII code value. For example, if the user enters E , then the output must be 69 , because 69 is the ASCII code value for uppercase E.

    C Program to print the ASCII value of a character

    #include <stdio.h>
    
    int main() 
    {
        char ch;
        int ascii;
        printf("Enter a Character: ");
        scanf("%c", &ch);
    
        //convert the character into corresponding ASCII code
        ascii = ch;
    
        printf("The ASCII value of character %c is %d", ch, ascii);
    	return 0;
    	
    }

    Output

    Enter a Character: V
    The ASCII value of character V is 86

    C++ Program to print the ASCII value of a character

    #include<iostream.h>
    #include< conio.h>
    #include<math.h>
    void main()
    {
        clrscr();
        int ascii;
        char character;
        cout<<"Enter a character: ";
        cin>>character;
    
        //convert the character into corresponding ASCII code
        ascii = character;
        
        cout<<"The ASCII value for "<<character<<" is "<<ascii;
        getch();
    }

    Output

    Enter a character: (
    The ASCII value for ( is 40

    Python Program to print the ASCII value of a character

    character = input("Enter a character: ")
    print("The ASCII value for",character,"is",ord(character))

    Output:

    Enter a character: (
    The ASCII value for ( is 40

    Wrapping Up!

    In C and C++ when we assign a character data identifier type to an integer data type, due to implicit type conversion the compiler converts the individual character data type to its equivalent ASCII value. But in Python, we have a dedicated function ord() that can convert a character to its ASCII code. There are many characters in programming languages and every character has its own ASCII value, that remains same for all the programming languages and these ASCII values include digits, lowercase letter, uppercase letters, and special symbols.

    People are also reading:

    Leave a Comment on this Post

    0 Comments