In this post, we will learn C and C++ Print ASCII Values of single or all characters. The user will enter a character and will find out ASCII values of entered character. To Find out ASCII values of characters in the range loop are used. We will cover finding ASCII of character is entered by the user in C and C++, C++ programs ASCII value of string character, Find ASCII values of all characters
1. C Program to Find ASCII Values for all characters
The ASCII value of characters Falls in the range of 65 to 122 . Where 65 represents the upper case character A and 90 characters Z.To find out all charcater ASCII values run we have to run a loop starting for 65 till 122.
- The range from 65 to 90 is for upper case letters: A to Z.
- The range from 91 to 96 is the ASCII value of a special character
- The range from 97 to 122 is for lower case letters: a to z.
C Program to Find ASCII Values for all characters
#include <stdio.h>
int main() {
int val = 0;
printf("All Character \t ASCII Value\n\n");
for (val = 65; val <=122; val++) {
printf("%c = %d\n", val, val);
}
return 0;
}
Output
All 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
[ = 91
\ = 92
] = 93
^ = 94
_ = 95
` = 96
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
- Simple MENU Program in C++
- C and C++ Print ASCII Values of single or all characters
- Hello world Program in C++
- How to get user input in C++(5 ways)
- Convert String to Hex in C++ using std::hex
2. C Program to Find ASCII Values of a character entered by the user
Sometimes instead of printing the ASCII values of all letters, we have to print the letter entered by the user. In this example, we are asking the user to enter the character to find the ASCII value.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("ASCII value of %c = %d", ch, ch);
return 0;
}
Output
Enter a character: A
ASCII value of A = 65
3. C++ Program to Find ASCII Values for all characters
In this example, we are printing the ASCII value of the upper case letter, lower case letter, and special characters whose range starts from 65 to 122. To print all we are looping start from 65 till 122.
C++ Program Example
#include <iostream>
using namespace std;
int main() {
int val = 0;
char ch;
cout<<"The ASCII value of all charcters:\n ";
for (val = 65; val <=122; val++) {
ch = val;
cout<<ch<<" = "<<val<<endl;
}
return 0;
}
Output
The ASCII value of all charcters:
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
[ = 91
\ = 92
] = 93
^ = 94
_ = 95
` = 96
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
4. C++ Program to Find ASCII Values single character entered by the user
Sometimes instead of printing the ASCII values of all letters, we have to print the letter entered by the user.In this C++ program example, we are asking the user to enter the character to find the ASCII value and finding the ASCII value of entered character printing the result.
#include <iostream>
using namespace std;
int main() {
int val = 0;
char ch;
cout<<"Enter the Character: ";
cin>>ch;
val = ch;
cout<<"The ASCII value of "<<ch<<" = "<<val<<endl;
return 0;
}
Output
Enter the Character: A
The ASCII value of A = 65
5. C++ Program to Find ASCII value of all characters in a string
In this C++ program, we are asking users to enter a string value and we are iterating each character of string using a while loop and printing its character.
C++ Program Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string str_domain;
char ch;
int x=0, val;
cout<<"Please Enter String: ";
getline (cin, str_domain);
cout<<"\nCharacter\t\tASCII Value\n"<<endl;
while(str_domain[x])
{
ch = str_domain[x];
val = ch;
cout<<ch<<"\t\t\t"<<val<<endl;
x++;
}
return 0;
}
Output
Please Enter String: devenum.com
Character ASCII Value
d 100
e 101
v 118
e 101
n 110
u 117
m 109
. 46
c 99
o 111
m 109