C Program to find ASCII value of a character
ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard that assigns numerical values to represent letters, digits, and other symbols used in computers and telecommunication devices.
In ASCII, each character is assigned a unique 7-bit code (later extended to 8-bit) that represents a specific symbol. For example, the uppercase letter "A" is represented by the code 65 (in decimal), while the lowercase letter "a" is represented by the code 97. Other common characters, such as numbers, punctuation marks, and control characters, are also assigned specific codes.
The ASCII code is widely used in computer systems and communication protocols to encode and transmit text information. However, it has some limitations, such as the inability to represent characters from non-Latin scripts, which led to the development of other encoding standards like Unicode.
Here's a C program that displays the ASCII value of a single character variable:
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("The ASCII value of %c is %d", c, c);
return 0;
}
Output:
Enter a character: g
The ASCII value of g is 103
This program first declares a variable c of type char, which can hold a single character. It then prompts the user to enter a character using the printf function and reads the input using the scanf function.
Finally, it displays the ASCII value of the entered character using the printf function, where %c is replaced by the entered character and %d is replaced by its ASCII value.
You can compile and run this program on any C compiler to test it out.
Here's a C program that displays the ASCII value of all the characters:
#include <stdio.h>
int main() {
int i;
for(i = 33; i<= 126; i++) {
printf("The ASCII value of %c is %d\n", i, i);
}
return 0;
}
Output:
The ASCII value of !is 33
The ASCII value of " is 34
The ASCII value of # is 35
The ASCII value of $ is 36
The ASCII value of % is 37
The ASCII value of & is 38
The ASCII value of ' is 39
The ASCII value of ( is 40
The ASCII value of ) is 41
The ASCII value of * is 42
The ASCII value of + is 43
The ASCII value of , is 44
The ASCII value of - is 45
The ASCII value of .is 46
The ASCII value of / is 47
The ASCII value of 0 is 48
The ASCII value of 1 is 49
The ASCII value of 2 is 50
The ASCII value of 3 is 51
The ASCII value of 4 is 52
The ASCII value of 5 is 53
The ASCII value of 6 is 54
The ASCII value of 7 is 55
The ASCII value of 8 is 56
The ASCII value of 9 is 57
The ASCII value of : is 58
The ASCII value of ; is 59
The ASCII value of < is 60
The ASCII value of = is 61
The ASCII value of > is 62
The ASCII value of ?is 63
The ASCII value of @ is 64
The ASCII value of A is 65
The ASCII value of B is 66
The ASCII value of C is 67
The ASCII value of D is 68
The ASCII value of E is 69
The ASCII value of F is 70
The ASCII value of G is 71
The ASCII value of H is 72
The ASCII value of I is 73
The ASCII value of J is 74
The ASCII value of K is 75
The ASCII value of L is 76
The ASCII value of M is 77
The ASCII value of N is 78
The ASCII value of O is 79
The ASCII value of P is 80
The ASCII value of Q is 81
The ASCII value of R is 82
The ASCII value of S is 83
The ASCII value of T is 84
The ASCII value of U is 85
The ASCII value of V is 86
The ASCII value of W is 87
The ASCII value of X is 88
The ASCII value of Y is 89
The ASCII value of Z is 90
The ASCII value of [ is 91
The ASCII value of \ is 92
The ASCII value of ] is 93
The ASCII value of ^ is 94
The ASCII value of _ is 95
The ASCII value of ` is 96
The ASCII value of a is 97
The ASCII value of b is 98
The ASCII value of c is 99
The ASCII value of d is 100
The ASCII value of e is 101
The ASCII value of f is 102
The ASCII value of g is 103
The ASCII value of h is 104
The ASCII value of i is 105
The ASCII value of j is 106
The ASCII value of k is 107
The ASCII value of l is 108
The ASCII value of m is 109
The ASCII value of n is 110
The ASCII value of o is 111
The ASCII value of p is 112
The ASCII value of q is 113
The ASCII value of r is 114
The ASCII value of s is 115
The ASCII value of t is 116
The ASCII value of u is 117
The ASCII value of v is 118
The ASCII value of w is 119
The ASCII value of x is 120
The ASCII value of y is 121
The ASCII value of z is 122
The ASCII value of { is 123
The ASCII value of | is 124
The ASCII value of } is 125
The ASCII value of ~ is 126
This program uses a for loop to iterate through all the ASCII characters from 0 to 255. Inside the loop, it uses the printf function to display the ASCII value of the current character, where %c is replaced by the current character and %d is replaced by its ASCII value.
You can compile and run this program on any C compiler to test it out. It will display the ASCII value of all the characters from 0 to 255. Note that some of these characters may not be printable or displayable on your console or terminal.