>> SPEAKER: Recall that everything underneath the hood of a computer is
stored using bits.
And bits, in turn, can be used to represent numbers.
And numbers, in turn, can be used to represent characters.
In fact, there exists a conventional mapping between those numbers and
letters, called ASCII--
American Standard Code for Information Interchange.
>> Now with C, it turns out that we can see this equivalence, because we not
only have ints, we also have chars, both of which at the end of the day
are represented as numbers and, in turn, bits.
So let's write a simple program that simply tells me what the mapping is
between numbers and letters, keeping in mind that 65 is a capital A and 97
is a lower case a.
Let's begin.
>> "include standard I/O dot h." "int main void." And now I'd like to
iterate over all of the capital letters printing out their numeric and
character equivalents.
So for this, I'll use a "for" loop.
"for int i gets--
and now, rather than start at the usual 0, why don't I start at a value
I know to be significant, like 65 for capital A?
Let's do this, so long is i is less than--
um--
65 plus 26, because I know there are 26 letters in the alphabet.
And then on each iteration of this loop, let me increment i by 1.
>> Now, on each iteration of this loop, what do I want to do?
I'd like to print out what the current number is i and what the
corresponding char is.
Now, to achieve that, I can cast, so to speak, the int to a char in the
following way.
"print f %i is %c backslash n" In other words, I want to say, this
number is this character.
So I need to plug in two values to this place holders for print f, so
I'll do comma i, comma--
>> Now, I don't have a variable c or a char, but I do have a number.
And I know that numbers can be mapped to letters, I simply need to tell the
computer to do that.
And so I can cast i from an int to a char, simply by specifying in
parentheses that I'd indeed like to convert it to a char.
>> Let's close now this statement, save the file, and compile this program.
"make ascii 0 dot slash ascii 0." And very quickly print it to the screen as
this mapping between numbers and their character equivalents.
In fact, if I scroll back up, I first see that 65 is A, 66 is B, and if I
scroll back down, 90 is Z.