Program to print ASCII Value of a character (original) (raw)
Last Updated : 13 Nov, 2025
ASCII (American Standard Code for Information Interchange) assigns a numerical value to each character. In programming, we often need to get the ASCII value of a character.
**Examples:
**Input: a
**Output: 97**Input: D
**Output: 68
**Explanation: The ASCII values of 'a' and 'D' are 97 and 68 respectively.
Below are few methods in different programming languages to print ASCII value of a given character :
In Python
Python provides the built-in ord() function to get the ASCII value of a character.
Python `
c = 'g' print(ord(c))
`
In C
In C, the %d format specifier can be used to print the ASCII value of a character:
CPP `
#include <stdio.h> int main() { char c = 'k'; printf("%d",c); return 0; }
`
In C++
In C++, we can use the int() function to convert a character to its ASCII value:
C++ `
#include using namespace std;
int main() { char c = 'A'; cout <<int(c); return 0; }
`
In Java
In Java, assigning a char to an int variable gives its ASCII value:
Java `
public class AsciiValue { public static void main(String[] args) { char c = 'e'; int ascii = c; System.out.println(ascii); } }
`
In C#
The concept to print ascii in c# is similar to that of Java:
C# `
using System;
public class AsciiValue { public static void Main() { char c = 'e'; int ascii = c; Console.WriteLine(ascii); } }
`
In JavaScript
JavaScript provides the charCodeAt() method to get ASCII value:
JavaScript `
val = 'A' console.log(val.charCodeAt(0));
`