C programming language uses ASCII codes to represent characters. ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns unique numeric codes to each character. In this article, we will discuss ASCII values in C programming language, their usage, and examples.
An ASCII value is a unique numeric code assigned to each character in the ASCII character set
(toc)
ASCII Values in C
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns a unique numerical
Accessing ASCII Values
To access the ASCII value of a character in C, you can simply treat the character as an integer. This is because characters are stored as integers under the hood.
Example:
char ch = 'A';
int ascii_value = ch;
printf("ASCII value of 'A' is: %d\n", ascii_value);
Output:
ASCII value of 'A' is: 65
Converting ASCII Values to Characters
You can convert an ASCII value back to its corresponding character using type casting:
int ascii_value = 65;
char ch = (char)ascii_value;
printf("Character corresponding to ASCII value 65 is: %c\n", ch);
Output:
Character corresponding to ASCII value 65 is: A
ASCII Table
The ASCII table lists the characters and their corresponding numerical values. You can find a complete ASCII table online.
Common ASCII Values:
'A'
to'Z'
: 65 to 90'a'
to'z'
: 97 to 122'0'
to'9'
: 48 to 57'\n'
: 10 (newline)'\t'
: 9 (tab)
Using ASCII Values
ASCII values can be used for various purposes, such as:
- Character conversion: Converting between uppercase and lowercase letters.
- String manipulation: Implementing string functions like
strlen
,strcmp
, andstrcpy
. - Character encoding and decoding.