C and C++ Print ASCII Values of single or all characters

C-Plus-Plus-Programs

In this post, we are going to learn how in C and C++ Print ASCII Values of single or all characters. The ASCII of character is entered by the user in C and C++ programs and value of any string character ASCII value.

1. C Program to Print ASCII Values all of characters


The ASCII value starts from 65 for the upper case letter A and 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

#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

2. C Program to ASCII Values of single character


Sometimes instead of printing the ASCII values of all letters, we have to print the letter enter by the user. In this example, we are asking the user to enter the character to find the ASCII value.

C Program

#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 ASCII Values all characters


In this example, we are printing the ASCII value of the upper case letter, lower case letter, and special character.

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. ASCII Values single enter character


Sometimes instead of printing the ASCII values of all letters, we have to print the letter enter 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.

C++ Program

#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 print ASCII value of all character in 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