Manuel posted:
well, I thought about using the variable of type byte because I need to
store values from 0 to 255.
C++ has a series of integer types, all of which come in a signed and
unsigned flavour:
char
short
int
long
The fundamental differences between them are:
(1) Their range:
rangeof(signed char) is a subset of rangeof(signed short),
which is a subset of rangeof(signed int), which is a subset of rangeof
(signed long).
rangeof(unsigne d char) is a subset of rangeof(unsigne d short),
which is a subset of rangeof(unsigne d int), which is a subset of rangeof
(unsigned long).
Note that this does not forbid them from all having the same range -- the
following could very well be true on a given system:
rangeof(signed char)==rangeof( short)==rangeof (int)==rangeof( long)
Note though that it's possible that the upper limit of "unsigned char" be
higher than the upper limit of "long".
(2) The amount of memory they occupy.
Put quite simply:
sizeof(signed char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
(3) How efficiently the system deals with them (i.e. how fast it can work
with them)
The fastest type should be int, as it's supposed to be the "natural"
integer size for the system.
Now that we know a few differences between the integer types, we can make
an educated decision as to which to use for each purpose. Before we go any
further however, we should note that the Standard guarantees a minimum
range for each type:
(1) unsigned char : 0 through 255
(2) unsigned short : 0 through 65 535
(3) unsigned int : 0 through 65 535
(4) unsigned long : 0 through 4 294 967 295
(1) signed char: -127 through 127
(2) signed short: -32 767 through 32 767
(3) signed int: -32 767 through 32 767
(4) signed long: -2 147 483 647 through 2 147 483 647
The first thing to consider is whether we shall be storing negative
numbers; if so, we want a "signed" type. The first choice should be "signed
int" or "unsigned int":
int i = -3;
unsigned j = 5;
The next thing to consider is whether the number we store might fall
outside the guaranteed range of "signed int" or "unsigned int". If it does,
then we should "unsigned long" or "signed long":
long i = -70000;
long unsigned i = 70000U;
(If you're writing a positive integer literal which is larger than 65 535,
then append "U" to it.)
If the number is within range of "int" or "unsigned", then the last thing
to consider is whether memory is scarce. If we want to define an array of
87 thousand integers, we might consider using a "char" or a "short" -- it
will be less efficient, but it will use only half or a quarter of the
memory (depending on the system).
One last thing: If you don't specify the signedness of a type, then it
defaults to signed (i.e. "long" == "long signed"). However this isn't quite
true for "char". A plain char might be signed or unsigned, depending on the
system, so it isn't any use for doing arithmetic or storing numbers; it
should only be used for storing characters. If you want a signed char, then
make sure you specify "signed":
char unsigned a = 5; /* This is unsigned */
char signed b = -5; /* This is signed */
char c = 2; /* This could be either */
If you really want to become expert at working with integer types in C and
C++, then you might also want to learn about "integer promotion".
To answer your question though: If you want to store a number in the range
0 through 255, then just use "unsigned":
unsigned i = 255;
Only consider using "char" or "short" if memory is scarce, e.g.:
char unsigned array[99999999];
--
Frederick Gotham