Congrats! So now the numbers are clear for you, at least the decimal form of them.
How about printing a character? Let’s do it:
#include <stdio.h>
int main(int argc, char *argv[]){
printf("My letter is %c.\n", 'A');
return 0;
}
Now let’s make a “typo” and make it print a “decimal” instead of a “character” or “letter”:
#include <stdio.h>
int main(int argc, char *argv[]){
printf("My letter is %d.\n", 'A');
return 0;
}
Huh? Your letter is what? O_o
Hello, ASCII! (search for “ascii art” or “bbs acid” to see what sort of trippy childhood some of us could possibly have!)
Just remember the 65 and you’re good to go. (No, it’s not 1995 anymore so you won’t get any credits for writing your name in ASCII or reading them backward :D)
So what’s the link between char
and int
? Well, an int
eger variable(a box)
can hold a “letter” inside it.
If you’re good with math(since I’m not), think of it as 4 > 1 and if you’re interested in history, go back and check the last post about the “order” of data types.
You may ask, “Am I allowed to store a decimal number inside a char
variable?”
The answer is “Yes, indeed!” and in C
, you have the same level of freedom as
shooting yourself in the head while jumping off a tower.
It IS perfectly possible but not really recommended, specially using the char
keyword.[1] Why? Simply because, C codes are written for “humans” and if
someone is looking at your code for the first time(eventually last one too!),
s/he will expect that variable to hold an “English letters” rather than some
gibberish!
Still don’t get it? That’s fine, I just want to make you more confused so you’ll go and learn:
- Why there are some ASCII codes for digits(48 to 57) if
char
is simply nothing but a container for the “Lil int”s?
[1] There are some “libraries” out there for letting you having the 1-byte
integer(uint_8
), 2-bytes integer(uint_16
) and so on.