Your question is:
Why does it control all letters from a to z and how come my program ends if it's a little char instead of a capital?
The answer is, because of the while test, which tests whether input <'a'
or 'z'< input
.
Here is some background information that will help you understand why this happens.
In your program, input is a char
, and, according to the C standard, the char type is an integral type. This means that 'a'
(C's way to designate a char
literal) is, in fact, a number, and thus, it can be compared with comparison operators such as <
or >
to other (integral) numbers or other char
(here, the content of input
).
Now, what is the actual integral value of a char
? While the integral values of the character set are implementation-defined, in general, C compilers (including Visual Studio's) will use the ASCII Character Codes.
So:
'a'
in your code, refers to the integral value of ASCII code for the char
'a'
, which is 97,
- and
'z'
in your code refers to the integral value of ASCII code for the char
'z'
, which is 122
As you can see also from the ASCII table (ASCII Character Codes Chart 1 from MSDN), the alphabet a-z has consecutive code numbers ('a'
is 97, 'b'
is 98, etc.), and the lowercase alphabet is effectively an ASCII code from 97 to 122.
So, in your code, the test input <'a'
or 'z'< input
is equivalent to input < 96
or 122 < input
, and this will be true when the entered char
has any ASCII value outside the range of 96 - 122, meaning, any char that is entered which is not in the range of ASCII codes for lowercase letters from 'a' to 'z' will result in the while test being true, and repeating the scanf()
.
Finally, as noted by other commentators or contributors, the right type is char
, not Char
since C is case-sensitive.