Tipos Primitivos em C++

From Wiki**3

Revision as of 23:51, 27 February 2008 by Root (talk | contribs)

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.

Simple Atomic Types

In these 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).

Integers

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:

 int a;
 int b = 1;
 int c = 2, d = a, e = a + b;
 short s1 = 8;
 unsigned short s2 = 9;
 long l1 = 1;
 long int l2 = 2;
 long l3 = 3L;
 unsigned int u1 = 0;
 unsigned int u2 = -1;   // WARNING: will be converted to a large positive number
 unsigned long int ul1 = 1UL;
 unsigned long long ul2 = 1UL<<15;  // 1 bit shifted 15 places
 char c = 'c';
 unsigned char c2 = '\0';   // tricky...

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.

Floating Point Numbers

Floating point numbers conform to the IEEE 754 standard

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

Examples:

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

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

Boolean Values

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*).

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

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

Enumerations

Other Primitive Types

Pointers

Vectors