dylanthomasfan said:
I want to know how one can use
and manipulate byte-sized integers in C.
Byte-sized integears are useful where one uses them in arrays declared
in the heap, so it makes sense not to declare ints when we only need
bytes for flags, small counters etc. I know I could manipulate (char)
but that is too inelegant...
The byte size integer types are:
1 unsigned char
2 signed char
3 char
My policy is not to use lower ranking ( than int ) integer types
without a special reason.
A string or an array would be a special reason.
Saving space would be a special reason,
but only if I were writing something for a platform
where it I knew that it was possible to save space that way,
and only if I needed the space.
It's possible and somewhat likely, that in a non array context,
that an object of type char, will be allocated on an int boundary,
and more or less treated as an int with the higher
addressed bytes masked off, which not only takes up as
much space as an int, but is slower.
On my machine, the following program, prints out "4".
/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
char one, two;
printf("%d\n", (int)&one - (int)&two);
return 0;
}
/* END new.c */
Another thing about low ranking integer types,
is that they very frequently get promoted,
or otherwise converted, to type int, anyway,
in most arithmetic operations.
It's not uncommon for this coversion to be implemented
literaly in code translation.
If you really want to save space implementing flags,
use bits of an object of type unsigned.
For arithmetic with small numbers, type int is just fine.