C Program
#include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf("%c", &ch); printf("ASCII value of %c = %d", ch, ch); return 0; }
C Output
Input: A
Output: ASCII value of A = 65
C++ Program
#include <iostream> using namespace std; int main() { char ch; cout << "Enter a character: "; cin >> ch; cout << "ASCII value of " << ch << " = " << int(ch); return 0; }
C++ Output
Input: z
Output: ASCII value of z = 122
JAVA Program
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a character: "); char ch = sc.next().charAt(0); System.out.println("ASCII value of " + ch + " = " + (int) ch); } }
JAVA Output
Input: M
Output: ASCII value of M = 77
Python Program
ch = input("Enter a character: ")[0] print(f"ASCII value of {ch} = {ord(ch)}")
Python Output
Input: k
Output: ASCII value of k = 107
Explanation
Example
If you enter the character A, the program prints 65 because that is the decimal value of A in the ASCII table. ASCII (American Standard Code for Information Interchange) represents every character, symbol, or control key with a single integer code. This correspondence enables text to be stored and transmitted efficiently by computers.
Real-Life Analogy
Imagine ASCII as a special ID card number for every character. Similar to how an individual's ID number is utilized in databases rather than his or her entire name, computers hold 65 rather than "A", conserving space and enhancing speed. During the display of text, the number is merely "translated" back into viewable letter.
Why It Matters
Knowing ASCII is important to any newcomer in programming, as text manipulation is a basic skill in nearly every program. Knowing how characters are stored is useful when working with encryption, encoding translation, or network data transfer. In embedded systems or low-level programming, ASCII values will usually be manipulated directly rather than characters.
Learning Insights
This lesson educates the basics regarding type casting, character manipulation, and concept of encoding. It also demonstrates how a character in C, C++, and Java may be manipulated as an integer when the situation demands it, and how Python makes it easy with the ord() function being built-in. This is an important concept while working with algorithms that involve alphabets, case conversion, or character-based arithmetic.
Real-World Applications
ASCII codes are employed in password verification (whether characters are upper-case, lower-case, or numeric), in file read/write operations, in cryptography algorithms, and even in graphics when writing text. ASCII math is also employed by programmers to transform characters, such as converting 'A' into 'a' by adding 32 to its ASCII code.
In summary, learning how to find the ASCII value of a character strengthens a programmer’s understanding of how text is stored and processed by computers. Whether you’re building a compiler, creating encryption logic, or working with binary data, this concept will prove useful. For beginners, mastering ASCII values opens the door to better control over text processing, making it easier to work with strings, file data, and network communications in any programming language.
Social Plugin