Alright actually I've study about how to use looping to make my code more efficient so that I could use a particular block of code that should be repeated without typing it over and over again, and after attempted to use what I've learn so far to program something, I feel it's time for me to proceed to the next chapter to learn on how to use control statement to learn how to instructs the program to make decision.
But the thing is that, before I advance myself to it, I still have a few question that need any expert's help on previous stuff. Actually it's about datatype.
A. Character Type
- I extract the following from the book C primer Plus 5th ed:
Somewhat oddly , C treats character constans as type
int
rather thanchar
. For example, on an ASCII system with a 32-bitint
and an 8-bitchar
, the code:char grade = 'B';
represents
'B'
as the numerical value 66 stored in a 32-bit unit,grade
winds up with 66 stored ub ab 8-bit unit. This characteristic of character constants makes it possible to define a character constant such as'FATE'
, with four separate 8-bit ASCII codes stored in a 32-bit unit. However , attempting to assign such a character constant to achar
variable results in only the last 8 bits being used, so the variable gets the value'E'
.
So the next thing I did after reading this was of course, follow what it mentions, that is I try store the word
FATE
on a variable withchar grade
and try to compile and see what it'll be stored usingprintf()
, but instead of getting the character'E'
printed out, what I get is'F'
.Does this mean there's some mistake in the book? OR is there something I misunderstood?
From the above sentences, there's a line says C treats character constants as type
int
. So to try it out, I assign a number bigger than255
, (e.x.356
) to thechar
type.Since
356
is within the range of 32-bitint
(I'm running Windows 7), therefore I expect it would print out356
when I use the%d
specifier.But instead of printing
356
, it gives me100
, which is the last 8-bits value.Why does this happen? I thought
char == int == 32-bits
? (Although it does mention before char is only a byte).
B. Int and Floating Type
I understand when a number stores in variable in
short
type is pass to variadic function or any implicit prototype function, it'll be automatically promoted toint
type.This also happen to floating point type, when a floating-point number with
float
type is passed, it'll be converted todouble
type, that is why there's no specifier for thefloat
type but instead there's only%f
fordouble
and%Lf
forlong double
.But why there's a specifier for
short
type although it is also promoted but notfloat
type? Why don't they just give a specifier forfloat
type with a modifier like%hf
or something? Is there anything logical or technical behind this?