Given a character, we need to print its ASCII value in C/C++/Java/Python.

Examples :
Input : a Output : 97 Input : D Output : 68
Here are few methods in different programming languages to print ASCII value of a given character :
- Python code using ord function :
ord() : It coverts the given string of length one, return an integer representing the unicode code point of the character. For example, ord(‘a’) returns the integer 97.
# Python program to print# ASCII Value of Character# In c we can assign different# characters of which we want ASCII valuec='g'# print the ASCII value of assigned character in cprint("The ASCII value of '"+c+"' is",ord(c))chevron_rightfilter_noneOutput:
The ASCII value of g is 103
- C code: We use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value.
// C program to print// ASCII Value of Character#include <stdio.h>intmain(){charc ='k';// %d displays the integer value of a character// %c displays the actual characterprintf("The ASCII value of %c is %d", c, c);return0;}chevron_rightfilter_noneOutput:
The ASCII value of k is 107
- C++ code: Here int() is used to convert character to its ASCII value.
// CPP program to print// ASCII Value of Character#include <iostream>usingnamespacestd;intmain(){charc ='A';cout <<"The ASCII value of "<< c <<" is "<<int(c);return0;}chevron_rightfilter_noneOutput:
The ASCII value of A is 65
- Java code : Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, Java converts the character value to an ASCII value.
// Java program to print// ASCII Value of CharacterpublicclassAsciiValue {publicstaticvoidmain(String[] args){charc ='e';intascii = c;System.out.println("The ASCII value of "+ c +" is: "+ ascii);}}chevron_rightfilter_none - C# code : Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, C# converts the character value to an ASCII value.
// C# program to print// ASCII Value of CharacterusingSystem;publicclassAsciiValue{publicstaticvoidMain(){charc ='e';intascii = c;Console.Write("The ASCII value of "+c +" is: "+ ascii);}}// This code is contributed// by nitin mittalchevron_rightfilter_noneOutput:
The ASCII value of e is 101
Recommended Posts:
- Count and Print the alphabets having ASCII value in the range [l, r]
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Minimize ASCII values sum after removing all occurrences of one character
- Count of alphabets having ASCII value less than and greater than k
- Convert the ASCII value sentence to its equivalent string
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Count pairs of characters in a string whose ASCII value difference is K
- Python | Ways to convert list of ASCII value to string
- Python - Sort Strings by Maximum ASCII value
- Print given sentence into its equivalent ASCII form
- Print each word in a sentence with their corresponding average of ASCII values
- Program to find the largest and smallest ASCII valued characters in a string
- Program to implement ASCII lookup table
- Program to find the product of ASCII values of characters in a string
- Map function and Dictionary in Python to sum ASCII values
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Average of ASCII values of characters of a given string
- Check if a string contains only alphabets in Java using ASCII values
- Count the number of words having sum of ASCII values less than and greater than k
- Count characters in a string whose ASCII values are prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nitin mittal

