Understanding Data Types in C++

How you represent an integer, a letter or a word in C++ differs. For example, representing the integer 23 requires the keyword int before the variable name:

int myNumber=23;

Representing the letter "L" requires the keyword char before the variable name:

char myLetter='L';

The keywords char and int along with float, bool and double are known as Data Types. A Data Type defines the set of possible values allowed in a variable and its memory size. These five data types constitute the fundamental data types in C++.

The Basic Integer Type: int

To represent an integer such as 201, -33, 1024, etc., C++ requires the keyword int before the variable name. For example:

int numberA=201;
int numberB=-33;
int numberC=1024;

The Floating Number Type: float

To represent a decimal number such as 21.3, 0.42, 0.10, etc., you must use the keyword float before the variable name. For example:

float numberA=21.3;
float numberB=0.42;
float numberC=0.10;

The Extended Precision Type: double

The double data type is similar to float but can store roughly twice as many significant digits than a float data type. To represent numbers such as -243.16, 2453345.2, C++ requires the keyword double before the variable name. For example:

double numberA=-243.16;
double numberB=2453345.2;

The Single Character Type: char

To represent a character such as 'a,' 'B,' '9', etc., use the keyword char. For example:

char myCharacterA='a';
char myCharacterB='B';
char myCharacterC='9';

The Boolean Data Type: bool

To represent the value of 0 or 1. Or true or false, use the keyword bool. For example:

bool myValueA=false;
bool myValueB=true;

Also, a data type specifies the memory allocated for the variable. The memory allocated is machine dependent, and it varies from machine to machine. For example, on my Mac:

  • An int takes 4 bytes of memory
  • A char takes 1 byte of memory
  • A float takes 4 bytes of memory
  • A double takes 8 bytes of memory
  • A bool takes 1 byte of memory

Hope this helps.

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.