Tipos Primitivos em C++

From Wiki**3

In terms of basic types, C++ is very similar to C, both in terms of provided types and their meaning. Even though C++ is not a C superset (there are areas in which they do not overlap), semantics of primitive types is the same.

All variables (of any type) may be preceeded by the const specifier, meaning that the value may not be changed after initialization (remember that initialization and assignment are distinct actions). The interaction between pointer and the const keyword is further discussed below.

Tipos Atómicos Simples

In this category, we can find types for describing atomic entities. The following types may be further modified by qualifiers, such as signal definition in integers and characters.

Note that, contrary to some languages (such as Java), primitive types tend to be machine-dependent (e.g. pointer size).

Inteiros

An integer is declared with keyword int. By default, it is signed, but it may be declared as unsigned. As in C, when declared unsigned, the whole length is available for representing the integer (between 0 and (2^nbits)-1); when signed, half the range is for negative numbers and the other half for positive numbers.

Further specifications correspond to the length of an integer. There are two specifiers: short and long. Some compilers define long long. These modifiers may be used without explicitly specifying the basic type int.

Characters in C++, as in C, are "small" 8-bit integers. These numbers index character tables such as ISO 8859-1 (a.k.a. ISO Latin 1) or ASCII. Nevertheless, they are integers and may be regarded as such.

The following examples illustrate various declarations and initializers, some of which are literals (a literal is a value directly written in the program) and some of which are expressions: <cpp>

 int a;
 int b = 1;
 int c = 2, d = a, e = a + b;

</cpp> <cpp>

 short s1 = 8;
 unsigned short s2 = 9;

</cpp> <cpp>

 long l1 = 1;
 long int l2 = 2;
 long l3 = 3L;

</cpp> <cpp>

 unsigned int u1 = 0;
 unsigned int u2 = -1;   // WARNING: will be converted to a large positive number

</cpp> <cpp>

 unsigned long int ul1 = 1UL;
 unsigned long long ul2 = 1UL<<15;  // 1 bit shifted 15 places

</cpp> <cpp>

 char c = 'c';
 unsigned char c2 = '\0';   // tricky...

</cpp>

Note that some types may behave in "surprising" ways (especially when using signed and unsigned numbers in the same expressions). Special care should be taken when comparing signed and unsigned integers.

The compiler automatically performs conversions between some of these types: from small types to larger types. Converting from large types to smaller ones (where possible loss of precision may occur), requires explicit conversions.

Números Reais

Floating point numbers conform to the IEEE floating-point standard

Declarations are made using two keywords: float, for single precision numbers; and double, for double precision numbers.

Examples: <cpp>

 float f = 1.0f;
 double d1 = 2.1;
 double d2 = 2.3e38;

</cpp> Conversions from integers to floating point numbers are performed automatically. The reverse requires explicit conversions.

Valores Booleanos

Contrary to the C programming language, C++ does have a boolean type (bool). However, boolean values are still integers (0 meaning false; other values meaning true).

Contrary to some other languages (notably, Java), C++ also allows integers where booleans are expected (e.g. conditions in if-then-else instructions or cycles).

Two values are defined for this type: true and false.

Examples: <cpp>

 while (true) { /* infinite cycle */ }
 while (1) { /* another infinite cycle */ }

</cpp>

Void

The void type is used for only two purposes: for indicating that a function does not return a value and for defining general pointers (void*). <cpp>

 // some function that does not return a value
 // typically performs actions with side effects
 void f() { /* ... */ }
 
 void *vp;  // "void pointer"

</cpp> Void pointers step by one byte (versus element size for pointers to other types).

Enumerações

Enumerations in C++ follow the C model: they are simply named integers. These names cannot be reused in other enumerations in the same program.

Examples: <cpp>

 enum A { VALUE1, VALUE2, VALUE3 };
 enum A e = VALUE3;

</cpp> Contrast these enumerations with their Java counterparts.

Outros Tipos Primitivos

Ponteiros

Vectores