datatypes - sluczak/cpp_by_example GitHub Wiki

Return to the previous lesson basic language syntax

Fundamental datatypes

In C++ programmer can both: use predefined datatypes available in C++ and define his own, new datatypes (structures and classes).

There are plenty of predefined datatypes, but some of them are really common

Common datatypes

  • int - 32bit, represents the typical integer value. Can store both: positive and negative values.

Range on common PC: -2147483648 to 2147483647

  • unsigned int - 32bit, represents the positive-only integer value.

Range on common PC: 0 to 4294967295

  • double - 64bit, represents the double-precision, floating point value. Can store both: positive and negative values.

Range on common PC: -1,8 × 10E 308 to 1,8 × 10E 308

  • bool - 8bit, represents the boolean logic value

Acceptable values: true, false

  • void - This type is used to indicate that function doesn't return any value. Can't be used for variables

It is a no-value type.

STL common types

Standard Templates Library (STL) introduced in basic language syntax contains plenty of useful datatypes. Some of them will be introduced in next chapters (ex. containers).

Now I will only introduce the string type, which has became one of the basic datatypes in C++.

string - type which represents the sequence of characters. It's length depends on the containings and is adjusted dynamicaly, during the program execution.

Strings can be concatenated, and have several functions which lets programmer to easily operate on string objects (comparing two strings, finding specific character in string, creating substring, etc). String value is enclosed between two quote marks (").

New type in C++ 11

  • auto - type inferred by compiler, based on the first value assigned to variable.

Less common datatypes

  • char, unsigned char - 8bit, represents an integer value, but it is rather used to store a single character

  • long, unsigned long - represents an integer value. May be larger than int, but commonly it has an equal size

  • short, unsigned short - 16bit, represents an integer value, but stores narrower range of values

  • float - 32bit, represents a single-precision floating point value. It is considered as depreciated nowadays.

Range on common PC: -3,4 × 10E 38 to 3,4 × 10E 38

Important

  1. please remember that variables may rollover if they exceed their range! For example if you assign the negative value to unsigned int, compiler won't report any warning, but value of variable will be different than assigned.
unsigned int b = -1; // results in rolling over to b = 4294967295;

variable b has rolled over to it's maximum value, because unsigned int can't store a negative value.

  1. C++ has a construct of an array of any objects. Arrays won't be introduced in this training, because usage of arrays has been discouraged in favour to STL containers.

Containers will be introduced in further chapters.

Proceed to the next lesson assignments and casting