ASCII Value in C | What is a ASCII code ? | ASCII Values in C | Common Uses of ASCII Values in C

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 Value in c

ASCII Basics

The ASCII standard was first published in 1963 and has since become a fundamental building block of modern computing. It uses 7 bits to represent characters, which means there are 128 possible characters in the ASCII table. These characters include:
  1. Control Characters: These characters have no visible representation but control how text is processed, such as newline, carriage return, and tab.


  2. 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.
ASCII Table

Here's a simplified representation of the ASCII table for the first 32 characters (control characters) and some common printable characters:
ValueCharacterDescription
0NUL (null)Null character
1SOHStart of Heading
2STXStart of Text
3ETXEnd of Text
4EOTEnd of Transmission
5ENQEnquiry
6ACKAcknowledgment
7BELBell
8BS (backspace)Backspace
9TABHorizontal Tab
10LF (newline)Line Feed
11VTVertical Tab
12FF (form feed)Form Feed
13CR (carriage return)Carriage Return
14SOShift Out
15SIShift In
16DLEData Link Escape
17DC1Device Control 1
18DC2Device Control 2
19DC3Device Control 3
20DC4Device Control 4
21NAKNegative Acknowledgment
22SYNSynchronous Idle
23ETBEnd of Transmission Block
24CANCancel
25EMEnd of Medium
26SUBSubstitute
27ESCEscape
28FSFile Separator
29GSGroup Separator
30RSRecord Separator
31USUnit 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

In C programming, you can represent characters using single quotes, like 'A', '1', or '%'. Under the hood, C stores these characters as their corresponding ASCII values. For example:
c
char 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:
c
char 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:
c
char 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.

Common Uses of ASCII Values in C

ASCII values are widely used in C programming for various purposes, including:

  1. Character Comparisons:You can compare characters based on their ASCII values to check for alphabetical order, perform case-insensitive comparisons, or determine character ranges.

    c
    char a = 'A'; char b = 'B'; if (a < b) { printf("A comes before B\n"); } else { printf("B comes before A\n"); }

  2. Converting Between Uppercase and Lowercase:By manipulating ASCII values, you can convert characters between uppercase and lowercase.

    c
    char uppercase = 'A'; char lowercase = uppercase + 32; // Converts 'A' to 'a' based on ASCII values

  3. Checking for Digit Characters: You can determine if a character is a digit by checking its ASCII value range.

    c
    char input = '5'; if (input >= '0' && input <= '9') { printf("The character is a digit.\n"); } else { printf("The character is not a digit.\n"); }

  4. Looping Through Characters: You can use ASCII values to iterate through characters in a range.

    c
    for (char c = 'A'; c <= 'Z'; c++) { printf("%c ", c); }

  5. Printing Special Characters: You can use escape sequences like '\n' and '\t' to print special characters based on their ASCII values.

    c
    printf("This is a newline: %c\n", '\n'); printf("This is a tab: %c\n", '\t');
  6. String Manipulation: ASCII values play a crucial role in string manipulation functions like comparing strings, searching for substrings, and sorting strings.


  7. File I/O: When reading or writing text files, ASCII values are used to encode and decode characters.


Extended ASCII

While the original ASCII standard uses 7 bits (0 to 127), extended ASCII sets use 8 bits (0 to 255) and include additional characters specific to certain languages and applications. For example, the ISO-8859-1 character encoding extends ASCII to include characters for Western European languages.

Unicode

As computing evolved, the limitations of ASCII became apparent, especially for representing characters from non-Latin scripts and languages. Unicode was developed as a universal character encoding standard that assigns unique code points to characters from all writing systems, including ASCII characters. Unicode uses 16 bits (UTF-16) or even 32 bits (UTF-32) to represent characters, allowing it to cover a vast range of characters

.Conclusion

ASCII values in C are essential for character manipulation and text processing. Understanding the numeric representations of characters allows you to perform a wide range of operations, from character comparisons to string manipulations. While ASCII is the foundation, modern computing relies on more extensive character encodings like Unicode to support the diversity of languages and symbols used worldwide. Nonetheless, ASCII values remain a fundamental concept for anyone working with character data in C programming.

Post a Comment

0 Comments