Question about Data Types

Mmmk. I have a question about data types. I am reading the book, C++ Primer Plus by Stephen Prata and I am confused about char, short, int, long, etc. If the size of the data types vary from computer to computer, how would I know which one to use?

Forgive my ignorance. I am still learning. ;)
Last edited on
Regarding the basic integral types .... From the C++ standards document...

Fundamental types

Objects declared as characters (char) shall be large enough to store any member of the implementation’s
basic character set.

If a character from this set is stored in a character object, the integral value of that character
object is equal to the value of the single character literal form of that character.

It is implementation defined whether a char object can hold negative values.
Characters can be explicitly declared unsigned
or signed. Plain char, signed char, and unsigned char are three distinct types.

A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment
requirements that is, they have the same object representation.

For character types, all bits of the object representation participate in the value representation.
For unsigned character types, all possible bit patterns of the value representation represent numbers.
These requirements do not hold for other types.

In any particular implementation, a plain char object can take on either the same values
as a signed char or an unsigned char; which one is implementation-defined.

There are four signed integer types: signed char, short int, int, and long int.
In this list, each type provides at least as much storage as those preceding it in the list.

Plain ints have the natural size suggested by the architecture of the execution environment ;
the other signed integer types are provided to meet special needs.

For each of the signed integer types, there exists a corresponding (but different) unsigned integer type:
unsigned char, unsigned short int, unsigned int, and unsigned long int,
each of which occupies the same amount of storage and has the same alignment requirements
as the corresponding signed integer type ; that is, each signed integer type has the same object representation
as its corresponding unsigned integer type.

The range of nonnegative values of a signed integer type is a subrange of the corresponding unsigned integer type, and the value representation of each corresponding signed/unsigned type shall be the same.

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number
of bits in the value representation of that particular size of integer.
size of char has not changed as far as I can remember (8bits).
Integers have changed from 16 bits to 32 bit and now will (soon) be 64 bits.
If the size of the data types vary from computer to computer, how would I know which one to use?
I'll tell you my general policy:
Non-Unicode characters and data buffers: char
Unicode characters: wchar_t
Boolean values: bool (obviously)
Common integer operations: long
Integer operations forbidding negative values: unsigned long
Offsets (e.g. for stepping through a buffer or std::vector): size_t

Note that wchar_t is not necessarily larger than char. You should be careful about that.
My rationale for using long is that most implementations I've seen make long as large as a machine register (e.g. 32 bits on 32-bit mode, and 64 on 64-bit mode). Again, this isn't a requirement, so always be careful and know your compiler.

size of char has not changed as far as I can remember (8bits).

sizeof(char)==1 by definition. This doesn't necessarily mean that it contains 8 bits. Only that it's one byte long ("byte"!="octet").

Integers have changed from 16 bits to 32 bit and now will (soon) be 64 bits.

There have been 64-bit integers for quite some time now, and also 128-bit integers.
Topic archived. No new replies allowed.