Translations of this page?:

Tipos de Dados do C++

Programadores de C++ tem acesso, ou podem se utilizar, dos 5 tipos de dados da linguagem C: 'void', 'int', 'float', 'double' e 'char'.

TipoDescrição
voidnão é associado a nenhum tipo de dado
intnúmero inteiro
floatnúmero fracionário (“ponto flutuante”)
doublenúmero fracionário de precisão dupla
charcaractere

Além destes, o C++ define mais dois: 'bool' e 'wchar_t'.

TipoDescrição
boolValor Booleano: 'true' ou 'false' (verdadeiro ou falso)
wchar_tcaractere wide (?)

Modificadores de Tipos

Vários desses tipos podem ser modificados usando-se as palavras-chave 'signed', 'unsigned', 'short' e 'long'. Quando um desses modificadores de tipo é usado sozinho (isto é, sem definir um tipo de dados), o tipo 'int' é considerado. Isto é, se você definir uma váriavel usando apenas o modificador 'short', o compilador vai interpretar como 'short int'. Uma lista completa dos tipos de dados possíveis segue abaixo (tipos equivalentes são mostrados na mesma linha):

tipos inteiros
bool
char
signed char
unsigned char
wchar_t
shortshort intsigned shortsigned short int
unsigned shortunsigned short int
intsignedsigned int
unsignedunsigned int
longlong intsigned longsigned long int
unsigned longunsigned long int
tipos fracionários
float
double
long double
tipos inteiros com suporte opcional
long longlong long intsigned long longsigned long long int
unsigned long longunsigned long long int

Tipos de tamanho e intervalos

O tamanho e o intervalo de qualquer tipo de dados é compilador e dependente da arquitetura. Você pode usar o operador sizeof para determinar o tamanho de qualquer tipo de dados (expresso em bytes). No entanto, muitas arquiteturas implementam tipos de dados de um tamanho padrão. ints e floats são normalmente 4-byte (32-bit), chars 1-byte (8-bit), e doubles são normalmente 8-byte (64-bit). Booleanos são freqüentemente implementados como tipos de dados 1-byte (8-bit), apesar de só usar um bit para armazenamento do valor, true ou false. O tipo long long é 8-byte (64-bit). O header <cfloat> (ou “float.h”), define os intervalos para os tipos numéricos de ponto flutuante, e o <climits> (ou “limits.h”) para os tipos numéricos inteiros.

Limits for numeric values are defined in the <limits> header. The templated values of limits provide system-dependant numerical representations of the C++ data types. Use the appropriate function given the data type as the template argument as shown in the table below. Note that numeric_limits can be overloaded for user-defined types as well.

Method or
constant
ReturnDescription
is_specializedbool
radixintbase of exponent
digitsintnumber of radix digits in mantissa
digits10intnumber of base 10 digits in mantissa
is_signedbool
is_integerbool
is_exactbool
min()<type>smallest number that can be respresented (not the most negative)
max()<type>largest number
epsilon()<type>inherent representation error value
round_error()<type>maximum rounding adjustment possible
infinity()<type>
quiet_NaN()<type>invalid number that does not signal floating point error
signaling_NaN()<type>invalid number that signals floating point error
denorm_min()<type>
min_exponentint
min_exponent10int
max_exponentint
max_exponent10int
has_infinitybool
has_quiet_NaNbool
has_signaling_NaNbool
has_denorm<type>_denorm_style
has_denorm_lossbool
is_iec559boolconforms to IEC-559
is_boundedbool
is_modulobool
trapsbool
tinyness_beforebool
round_stylefloat_round_style { round_to_nearest, … }

The most common usage is in bounds checking, to determine the minimum and maximum values a data type can hold. The following code prints out the minimum and maximum values for a short on the system it is run.

  #include <limits>
  std::cout << "Maximum short value: " << std::numeric_limits<short>::max() << std::endl;
  std::cout << "Minimum short value: " << std::numeric_limits<short>::min() << std::endl;

Reading Type Declarations

Simple type declarations are easy to understand:

  int i

However, it can be tricky to parse more complicated type declarations:

  double **d[8]              // hmm...
  char *(*(**foo [][8])())[] // augh! what is foo?

To understand the above declarations, follow three rules:

  1. Start at the variable name (d or foo in the examples above)
  2. End with the data type (double or char above)
  3. Go right when you can, and left when you must. (Grouping parentheses can cause you to bounce left.)

For example:

ExpressionMeaning
double **d[8];
double **d[8]; d is … double
double **d[8]; d is an array of 8 … double
double **d[8]; d is an array of 8 pointer to … double
double **d[8]; d is an array of 8 pointer to pointer to double

Another example:

ExpressionMeaning
char *(*(**foo [][8])())[]
char *(*(**foo [][8])())[] foo is … char
char *(*(**foo [][8])())[] foo is an array of … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to array of … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to array of pointer to char

For a much more detailed explanation, see Steve Friedl's excellent description of how to read C declarations at http://www.unixwiz.net/techtips/reading-cdecl.html.

 
• • • SitemapRecent changesRSScc