(C++) data type

January 7, 2018 ยท View on GitHub

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.

The range of each of these data types can be found with std::numeric_limits.

Advice

  • Ideally, a program should be statically type safe [1]
  • Use a consistent method (such as uppercase first letter) to distinguish type names [2].

References