In said:
I made a variable under unsigned short but displaying it using char the
value that doesnt change in fact is shown changed...
unsigned short a
a = 321;
printf("a = %d", (char)a);
the display is 'a = 63'...
why is that?
If you can't engage your brain, C programming is not for you.
Isn't it obvious, from your result, that 321 cannot be represented by the
type char on your implementation?
On the vast majority of hosted implementations, char is an 8-bit type.
One of these 8 bits may or may not be used as a sign bit. How many bits
do you need to represent the value 321?
how can I have the right display for a?
By not converting it to a narrower type before displaying it. Isn't it
obvious? What's less obvious is the right printf invocation, to get the
right answer in a portable way:
printf("a = %u\n", (unsigned)a);
The cast to unsigned is needed because, in a portable programming context,
you don't know to what type a is going to be promoted by the default
argument promotions: int or unsigned int. Since no conversion descriptor
can accept either int or unsigned int, you have to bypass the default
argument promotions by converting the value to a type that is no longer
subject to the default argument promotions.
In standard C, this is an issue only with variadic functions, like
printf. For other functions you can always provide a prototype, so that
the default argument promotions are no longer performed by the compiler,
when the function is called.
Dan