ASCII Value in C
What is a ASCII code?
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns a unique numeric value to each printable and non-printable character used in computers, communication equipment, and other devices that use text. In C programming, ASCII values are represented as integers, and you can use these values to perform various operations on characters. While explaining ASCII values in 2000 words may be quite extensive, I can provide a comprehensive overview.
ASCII Basics
Control Characters: These characters have no visible representation but control how text is processed, such as newline, carriage return, and tab.
- Printable Characters: These are the characters that are displayed on the screen when you type them, such as letters, numbers, punctuation marks, and some special symbols.
Value | Character | Description |
---|---|---|
0 | NUL (null) | Null character |
1 | SOH | Start of Heading |
2 | STX | Start of Text |
3 | ETX | End of Text |
4 | EOT | End of Transmission |
5 | ENQ | Enquiry |
6 | ACK | Acknowledgment |
7 | BEL | Bell |
8 | BS (backspace) | Backspace |
9 | TAB | Horizontal Tab |
10 | LF (newline) | Line Feed |
11 | VT | Vertical Tab |
12 | FF (form feed) | Form Feed |
13 | CR (carriage return) | Carriage Return |
14 | SO | Shift Out |
15 | SI | Shift In |
16 | DLE | Data Link Escape |
17 | DC1 | Device Control 1 |
18 | DC2 | Device Control 2 |
19 | DC3 | Device Control 3 |
20 | DC4 | Device Control 4 |
21 | NAK | Negative Acknowledgment |
22 | SYN | Synchronous Idle |
23 | ETB | End of Transmission Block |
24 | CAN | Cancel |
25 | EM | End of Medium |
26 | SUB | Substitute |
27 | ESC | Escape |
28 | FS | File Separator |
29 | GS | Group Separator |
30 | RS | Record Separator |
31 | US | Unit Separator |
After these control characters, the ASCII table continues with printable characters, starting with the space character (ASCII 32) and going up to the tilde character (ASCII 126)
.ASCII Values in C
cchar letter = 'A'; // Stores the ASCII value of 'A' (65) in the variable 'letter'
char digit = '7'; // Stores the ASCII value of '7' (55) in the variable 'digit'
char symbol = '%'; // Stores the ASCII value of '%' (37) in the variable 'symbol'
You can also use integer literals to directly assign ASCII values to character variables:cchar newline = 10; // Assigns the ASCII value for newline to the variable 'newline'
char tab = 9; // Assigns the ASCII value for tab to the variable 'tab'
Conversely, you can obtain the ASCII value of a character using its integer representation. For example:cchar letter = 'A';
int asciiValue = (int)letter; // Stores 65 (ASCII value of 'A') in 'asciiValue'
This allows you to manipulate characters and perform various operations based on their ASCII values.ASCII values are widely used in C programming for various purposes, including:
Character Comparisons:You can compare characters based on their ASCII values to check for alphabetical order, perform case-insensitive comparisons, or determine character ranges.
cchar a = 'A'; char b = 'B'; if (a < b) { printf("A comes before B\n"); } else { printf("B comes before A\n"); }Converting Between Uppercase and Lowercase:By manipulating ASCII values, you can convert characters between uppercase and lowercase.
cchar uppercase = 'A'; char lowercase = uppercase + 32; // Converts 'A' to 'a' based on ASCII valuesChecking for Digit Characters: You can determine if a character is a digit by checking its ASCII value range.
cchar input = '5'; if (input >= '0' && input <= '9') { printf("The character is a digit.\n"); } else { printf("The character is not a digit.\n"); }Looping Through Characters: You can use ASCII values to iterate through characters in a range.
cfor (char c = 'A'; c <= 'Z'; c++) { printf("%c ", c); }Printing Special Characters: You can use escape sequences like '\n' and '\t' to print special characters based on their ASCII values.
cprintf("This is a newline: %c\n", '\n'); printf("This is a tab: %c\n", '\t');String Manipulation: ASCII values play a crucial role in string manipulation functions like comparing strings, searching for substrings, and sorting strings.
File I/O: When reading or writing text files, ASCII values are used to encode and decode characters.
0 Comments