1

I am reading some material on C++ and it said that to represent someone's age, int16_t is preferred over uint8_t, even though int16_t has a range of -32,768 to 32,767 and uint8_t has a range of 0 to 255.

A persons age can never be negative, so why is int16_t a better option?

Fingers
  • 373
  • 3
  • 18
  • Provide a link to the material, otherwise the question is difficult to answer without pure opinion. – President James K. Polk Apr 25 '16 at 23:27
  • Read some more of your book or whatever material. You are asking us "why does this author have this opinion". Nobody knows except for that author – M.M Apr 25 '16 at 23:27
  • 1
    I know `uint8_t`, but I don't know `uint_8t`. Are you sure you want to use `uint_8t`? – MikeCAT Apr 25 '16 at 23:27
  • I've seen people on the committee strongly discourage use of unsigned types for things otherthan bitfields. Wrt ages specifically. the author probably means you should allow `-1` to indicate something like "age not set." but this is just my speculation – Ryan Haining Apr 25 '16 at 23:28
  • 1
    maybe the author is concerned about `cout << age` which may invoke the character-type overload of `<<` on some systems . (But if so, changing the type of `age` to `int16_t` is not a great solution) – M.M Apr 25 '16 at 23:37
  • 1
    I voted to reopen. There are sound reasons not to represent a person's age as a `uint8_t`. And there are also sound reasons not to represent it with a `int16_t`. But I will stop short of trying to stuff a complete answer into the space of a comment. I've also upvoted the question. This is a good beginners' question that sometimes even seasoned C++ programmers get wrong. – Howard Hinnant Apr 26 '16 at 00:28
  • 1
    Also voted to reopen. Classic example for using `int`: `T difference_in_ages = abs(age1 - age2);` works for signed types but not unsigned (as the `age2 > age1` situation yields a "bogus" positive number). Another: `for (Age i = 10; i >= 0; --i) ...`. I'm not arguing either way, but saying there are factual pros/cons to be listed, and "primarily opinion-based" is not a good reason for closing the Q. – Tony Delroy Apr 26 '16 at 00:31

2 Answers2

1

There may be a couple of reasons for this choice.

For example, if your program compares birth date to current date you will eventually run into cases where the uint8_t will overflow if your program is in use long enough.

Or if the person is dead, or has an invalid age you may wish to set a negative number as a form of error, -1 for not set, -2 for dead, -3 for out of range, etc.

On some platforms cout will treat uint8_t as a char when printing, causing confusing output to those who do not expect it. ( @M.M pointed this out, More Info )

This approach also allows you more flexibility with the allowance of negative numbers / larger values. Basically in the general case memory is quite cheep, specially when dealing with 8 bits.
It's better to write code that has less chance of overflowing and is more flexable than to worry about packing more data into a smaller space.

Community
  • 1
  • 1
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • re. the first para, that would only occur if they also used `uint8_t` for storing date ranges, and there's no suggestion of that in the OP. (Although it is not a very clear post) – M.M Apr 25 '16 at 23:36
  • @M.M I was more thinking, `age = date1 - date2` where `age` is the `uint8_t`, though... Maybe I wasn't clear enough on that point? – Serdalis Apr 25 '16 at 23:37
0

There shouldn't be any reason to use an int_16 over a uint_8t unless your chipset is a 16 bit chip, where 16 bit operations would run more efficiently than 8 bit ones, although now days most people would never use a 16 bit chip, even in embedded systems programming.

user253751
  • 57,427
  • 7
  • 48
  • 90
Cate
  • 1,227
  • 2
  • 9
  • 7
  • I've removed the first sentence since it seems irrelevant to your actual answer - but I'm just a person like you, so if you disagree, you can put it back. – user253751 Apr 25 '16 at 23:34