T
Tomás
When you simply want to store a number, what integral type do you use?
For instance, let's say we have the following in a Poker game:
struct Card
{
enum Suit { Hearts, Diamonds, Spades, Clubs } suit;
int value;
};
Looking at the "value" variable above, it's range needs to be 0 to 51.
How many people would use an "int"? How many people would use an
"unsigned char"?
Upon first thought, I would presume that the most appropriate choice
would be "unsigned char", as the number shall always be positive, and it
has sufficient range.
I myself always use unsigned integral types unless I strictly need a
signed type. For instance:
unsigned GetDaysInMonth( unsigned month );
Nonetheless, I see that a lot of people use "int" everywhere, even when
the number shouldn't ever be negative.
Is it too pedantic to use an "unsigned char" to store the card's numeric
value? If so why?
Basically, what makes you choose a particular integral type?
Lastly, do you ever use an enum to get the desired range, as in:
enum CardValue { Ace = 1, King = 13 };
-Tomás
For instance, let's say we have the following in a Poker game:
struct Card
{
enum Suit { Hearts, Diamonds, Spades, Clubs } suit;
int value;
};
Looking at the "value" variable above, it's range needs to be 0 to 51.
How many people would use an "int"? How many people would use an
"unsigned char"?
Upon first thought, I would presume that the most appropriate choice
would be "unsigned char", as the number shall always be positive, and it
has sufficient range.
I myself always use unsigned integral types unless I strictly need a
signed type. For instance:
unsigned GetDaysInMonth( unsigned month );
Nonetheless, I see that a lot of people use "int" everywhere, even when
the number shouldn't ever be negative.
Is it too pedantic to use an "unsigned char" to store the card's numeric
value? If so why?
Basically, what makes you choose a particular integral type?
Lastly, do you ever use an enum to get the desired range, as in:
enum CardValue { Ace = 1, King = 13 };
-Tomás