A data type is the form that data (for example: numbers, words, images) has. Every variable has a data type. C++ is type safe, which means that during compiling it checks that all conversions are legal.
The example below the definition of a variable of data type double with the name of 'd' being assigned the value 3.1415. The next line tries to assign the text 'hello world' to d, which is illegal, because 'hello world' if not of data type double (but of std::string).
int main()
{
double d = 3.1415; //Legal
d = "hello world"; //ILLEGAL!
}
List of data types that are also keywords
Some data types are only accepted by some standards.
- C++98
- C++11
- bool
- char
- char16_t
- char32_t
- double
- float
- int
- long
- long long int
- short
- void (in the form of void*)
- wchar_t
The range of each of these data types can be found with std::numeric_limits.
- Ideally, a program should be statically type safe [1]
- Use a consistent method (such as uppercase first letter) to distinguish type names [2].
- [1] C++ Core Guidelines: P.4: Ideally, a program should be statically type safe
- [2] John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.7: 'Use a consistent method (such as uppercase first letter) to distinguish type names'