Evaluating date types of char

When reading my book i look at the following abbreviated example and ask when the program is checking the data type *char* in this example it can determine if a letter is greater or equal to 'A' and also less or equal to 'Z' however since they are non numeric how does it know that 'A' is less than 'Z'. When a variable is defined as *char* does it essentially break the variable defined as *char* into hex code and evaluate it from the hex codes which are numerical or am i missing something?

1
2
3
4
5
6
7
8
9
10
11
12
char letter(0);

cout << "Enter a letter";
cin >> letter;

if(letter >= 'A')
(
    if(letter <= 'Z')
    (
    )
)
Last edited on
Every ASCII character has a decimal/hex value.
So when you do a comparison with a char it uses that number.

If you want to see the values they are run this program:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main ()
{
    for (char ch = 'A'; ch <= 'Z'; ++ch) // This works because chars are byte sized integers.
        std::cout << ch << " : " << (int)ch << '\n';

    std::cout << "\n\n";

    for (char ch = 'a'; ch <= 'z'; ++ch) // This works because chars are byte sized integers.
        std::cout << ch << " : " << (int)ch << '\n';

    return 0;
}


$ ./charasdec.exe
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


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
That's essentially what i assumed. Does that mean that if your using the char type you could evaluate any ASCII character not just alphanumerics
I also have to ask is it coincidence that the upper-case and lower-case are exactly 32 characters apart, Or done specifically for this convenience. Seems that it would be somewhat easy to subtract or add the two to convert between them
Last edited on
That's essentially what i assumed. Does that mean that if your using the char type you could evaluate any ASCII character not just alphanumerics
ico

Yes there is a chart to see what the char evaluates to here: http://www.asciitable.com/


As for the 32 difference its related to binary values:
A: 65 == 01000001
a: 97 == 01100001

B: 66 == 01000010
b: 98 == 01100010

As you can see the bit for 32 in binary is set between the two numbers.
Topic archived. No new replies allowed.