ASCII Value of a Character in C

Get Certified in C Programming and Take Your Skills to the Next Level

Greetings to the world of programming enthusiasts! In this article, we’re embarking on an exciting journey into the heart of C programming. Our focus? Two fundamental concepts form the bedrock of text manipulation and character representation: the character data type and ASCII codes. Whether you’re a curious beginner or a seasoned coder seeking a refresher, join us as we delve into these fundamental principles that form the core of programming craftsmanship.

Character Data Types in C: Building Blocks of Text

The character data type is a fundamental concept in C programming that lays the foundation for working with individual characters, such as letters, digits, and symbols. C has several character data types like char, signed char and unsigned char, but char is the most commonly used for representing ASCII characters in C. Represented by the keyword ‘char,’ this data type enables a multitude of functionalities related to text manipulation, input/output operations, and string handling.

#include <stdio.h>
int main() {
char letter = 'A'; 
printf("The character is: %c\\n", letter);
return 0;
}

Output:

The character is A

Key Takeaways

  • ‘char’ stores characters using their corresponding ASCII codes in C.
  • Characters are enclosed within single quotes, e.g., ‘A,’ ‘5,’ or ‘$.’ Char variables can also be initialized with literal values like this.
  • Memory-wise, ‘char’ occupies a single byte, making it a compact data type.
  • It is integral to defining strings, which are essentially arrays of characters.

Decoding ASCII Codes in C: Unveiling Character Representations

The character encoding standard known as ASCII, or American Standard Code for Information Interchange, is used all over the world. Understanding ASCII codes is essential for converting characters into their appropriate numerical values in the world of C programming. Let’s put that into practice:

#include <stdio.h>


int main() {
    // In this program, we're running a loop from 0 to 126.
    // Inside the loop, we're displaying the character and its corresponding ASCII value.
    for (char character = 0; character <= 126; character++) {
        printf("Character: '%c', ASCII Value: %d \n", character, character);
    }


    return 0;
}

Output:

Character: ”, ASCII Value: 0
Character: ‘☺’, ASCII Value: 1
Character: ‘☻’, ASCII Value: 2
Character: ‘♥’, ASCII Value: 3

Character: ‘!’, ASCII Value: 33
Character: ‘”‘, ASCII Value: 34
Character: ‘#’, ASCII Value: 35
Character: ‘$’, ASCII Value: 36
Character: ‘%’, ASCII Value: 37
Character: ‘&’, ASCII Value: 38
Character: ”’, ASCII Value: 39
Character: ‘(‘, ASCII Value: 40
Character: ‘)’, ASCII Value: 41
Character: ‘*’, ASCII Value: 42
Character: ‘+’, ASCII Value: 43
Character: ‘,’, ASCII Value: 44
Character: ‘-‘, ASCII Value: 45
Character: ‘.’, ASCII Value: 46
Character: ‘/’, ASCII Value: 47
Character: ‘0’, ASCII Value: 48
Character: ‘1’, ASCII Value: 49
Character: ‘2’, ASCII Value: 50

Character: ‘9’, ASCII Value: 57

Character: ‘A’, ASCII Value: 65
Character: ‘B’, ASCII Value: 66
Character: ‘C’, ASCII Value: 67

Character: ‘Z’, ASCII Value: 90

Character: ‘a’, ASCII Value: 97
Character: ‘b’, ASCII Value: 98
Character: ‘c’, ASCII Value: 99

Character: ‘z’, ASCII Value: 122

Character: ‘{‘, ASCII Value: 123
Character: ‘|’, ASCII Value: 124
Character: ‘}’, ASCII Value: 125
Character: ‘~’, ASCII Value: 126

Key Points to Remember

  • ASCII codes in C range from 0 to 127, encompassing a total of 128 characters.
  • Common characters, including letters (uppercase and lowercase), digits, punctuation marks, and control characters, each have a distinct ASCII code.
  • For instance, the ASCII code for uppercase ‘A’ is 65, while lowercase ‘a’ corresponds to 97.
  • Complexity Analysis

Time complexity: O(1)
Auxiliary Space: O(1)

ASCII Table for Uppercase Character

CharacterASCII Value
A65
B66
C67
D68
E69
F70
G71
H72
I73
J74
K75
L76
M77
N78
O79
P80
Q81
R82
S83
T84
U85
V86
W87
X88
Y89
Z90

ASCII Table for Lowercase Character

CharacterASCII Value
a97
b98
c99
d100
e101
f102
g103
h104
i105
j106
k107
l108
m109
n110
o111
p112
q113
r114
s115
t116
u117
v118
w119
x120
y121
z122

Format Specifiers in C

When printing characters and strings in C, using the proper format specifiers like %c for chars and %s for strings in printf() is important. The wrong specifier can print the ASCII value instead of the intended character.

Character Arrays in C

C originally lacked robust string handling capabilities. Character arrays provided a way to manipulate strings by storing characters, like an array but without built-in string functions. They require null terminator ‘\0’ to mark the end.

Escape Sequences in C

Some characters like newline ‘\n’ and tab ‘\t’ have escape sequences representing them. These are used to insert special characters into strings and code.

Application of ASCII Codes in C Programming

Proficiency in working with ASCII codes is essential for various programming tasks:

  • Conversion of characters to their ASCII equivalents and vice versa.
  • Comparison and manipulation of characters based on their ASCII values.]
  • Implementation of encryption and decryption algorithms.
  • Handling input validation and executing text-based operations.

Real-world Scenario

Visualize creating a secure messaging program. Proficiency in ASCII codes empowers systematic character transformations, bolstering message security through encryption and decryption techniques. This mastery ensures that sensitive information remains intelligible only to intended recipients, enhancing overall communication privacy and reliability.

Conclusion

Through this exploration of C programming concepts, we’ve navigated the character data type and delved into the realm of ASCII codes. These foundational elements play a pivotal role in enabling efficient text manipulation and character representation. Armed with this understanding, you’re well-equipped to embark on your programming journey, employing character data types and ASCII codes in C to create dynamic and effective programs.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *