[ fromfile: cppintro.xml id: simpletypes ]
The simple types supported in C/C++ are listed in Table 1.2.
C++ simple types can (variously) be modified by the following keywords to produce other simple types.
short
long
signed
unsigned
[8]
Table 1.2. Simple Types Hierarchy
byte/char Types | Integral Types | Floating Point Types |
---|---|---|
bool | short int | float |
char | unsigned short | double |
signed char | int | long double |
unsigned char | unsigned int | |
wchar_t | anyType* | |
long int | ||
unsigned long |
You can also omit signed
from most types because that is the default.
Object size in C++ is expressed in terms of the size of char
.
the size of char
is 1.
the ANSI/ISO standard for C++ does not specify the size of any of the types displayed in Table 1.2.
It guarantees only that a given type (e.g., int
) must not be smaller than one that appears above it (e.g., short
) in the table.
Example 1.18 shows how sizeof()
can be used and some of the values it returns on a 32-bit x86 system.
Example 1.18. src/early-examples/size/qsize.cpp
#include <QString> #include <QTextStream> #include <QChar> #include <QDate> int main() { QTextStream cout(stdout); char array1[34] = "This is a dreaded C array of char"; char array2[] = "if not for main, we could avoid it entirely."; char* charp = array1; QString qstring = "This is a unicode QString. Much preferred." ; Q_ASSERT (sizeof(i) == sizeof(int)); cout << " c type sizes: \n"; cout << "sizeof(char) = " << sizeof(char) << '\n'; cout << "sizeof(wchar_t) = " << sizeof(wchar_t) << '\n'; cout << "sizeof(int) = " << sizeof(int) << '\n'; cout << "sizeof(long) = " << sizeof(long) << '\n'; cout << "sizeof(float) = " << sizeof(float) << '\n'; cout << "sizeof(double) = " << sizeof(double) << '\n'; cout << "sizeof(double*) = " << sizeof(double*) << '\n'; cout << "sizeof(array1) = " << sizeof(array1) << '\n'; cout << "sizeof(array2) = " << sizeof(array2) << '\n'; cout << "sizeof(char*) = " << sizeof(charp) << endl; cout << " qt type sizes: \n"; cout << "sizeof(QString) = " << sizeof(QString) << endl; cout << "sizeof(qint32) = " << sizeof (qint32) << "\n"; cout << "sizeof(qint64) = " << sizeof(qint64) << '\n'; cout << "sizeof(QChar) = " << sizeof (QChar) << endl; cout << "sizeof(QDate) = " << sizeof(QDate) << endl; cout << "qstring.length() = " << qstring.length() << endl; return 0; } Output: (example run on 32-bit system) sizeof(char) = 1 sizeof(wchar_t) = 4 sizeof(int) = 4 sizeof(long) = 4 sizeof(float) = 4 sizeof(double) = 8 sizeof(double*) = 4 sizeof(array1) = 34 sizeof(array2) = 45 sizeof(char*) = 4 qt type sizes: sizeof(QString) = 4 sizeof(qint32) = 4 sizeof(qint64) = 8 sizeof(QChar) = 2 sizeof(QDate) = 4 qstring.length() = 42
Pointer to first element of array. | |
Guaranteed to be 32 bits on all platforms. | |
Guaranteed to be 64 bits on all platforms. | |
Twice as big as a char | |
For # of bytes, be sure to take into account the size of QChar. |
Notice that all pointers are the same size, regardless of their type.
Initialization of Basic-Typed Variables | |
---|---|
When a variable is of a basic type, it must be initialized! Uninitialized variables of basic types have undefined values on program startup. They may have initial value 0 when running in a debugger and have garbage values when run in other envrionments. |
[8] For further discussion of the differences between signed an unsigned integral types, see Section 19.5.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |