Site icon DataFlair

ASCII Value of a Character in C

character data type in c

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

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

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

ASCII Table for Uppercase Character

Character ASCII Value
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90

ASCII Table for Lowercase Character

Character ASCII Value
a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122

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:

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.

Exit mobile version