What kind of variable to represent decimal number?
Isn’t this more like a general C question?
The simple answer would be that it should be big enough to hold the maximum value.
Also decide if the variable becomes signed or unsigned. Can it become negative? If it can, you must
use a signed type. If it is always positive, you may
still use a signed type, depending on the width needed.
You can decide to use a char, an short, an int or a long ( or the unsigned versions of these ).
Note that when you mix the usage of
s ( often seen in small embedded projects ), the compiler needs to insert lots of extra code to make things working.
Function parameters are often passed in single registers, there is no need to downscale the size of parameters to save memory:
/* Calling the first function may result in more code
than calling the second function: /
void foo(char count);
Here ‘count’ will get the width of a CPU register. */
void foo(int count);
Also care must be taken when signed-ness is mixed: the outcome of the expression
if( value_1 > value_2 )
depends on the the values and on the signed-ness of the two variables. You would ask for trouble if you compare signed with unsigned values.
In practice, the int
type has many advantages: it normally has the size of a CPU register ( 16, 32, or 64 bits ), and therefore, code using ( unsigned )
is very efficient. If all your variables can be stored in
s, you can keep your code really simple. The compiler won’t have to insert any (hidden) transformation of signed-ness or width.
Types with a known width:
/* defining types width a known width: */
Within the +TCP library, you will see an extensive usage of
, because that corresponds to the fields in the Internet packets.
And finally, consider using of
as often as applicable.