C++ Data Types

C++ Basic Data Types

As explained in the Variables chapter, a variable in C++ must be a specified data type:

Example

#include <iostream>
#include <string>
using namespace std;
 
int main () {
  // Creating variables
  int myNum = 7;               // Integer (whole number)
  float myFloatNum = 5.77;     // Floating point number
  double myDoubleNum = 9.99;   // Floating point number
  char myLetter = 'A';         // Character
  bool myBoolean = true;       // Boolean
  string myString = "Hello";   // String
   
  // Print variable values
  cout << "int: " << myNum << "\n";
  cout << "float: " << myFloatNum << "\n";
  cout << "double: " << myDoubleNum << "\n";
  cout << "char: " << myLetter << "\n";
  cout << "bool: " << myBoolean << "\n";
  cout << "string: " << myString << "\n";
 
  return 0;
}

Output—->

int: 7
float: 5.77
double: 9.99
char: A
bool: 1
string: Hello


Basic Data Types

The data type specifies the size and type of information the variable will store:

Data TypeSizeDescription
int4 bytesStores whole numbers, without decimals
float4 bytesStores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
double8 bytesStores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
boolean1 byteStores true or false values
char1 byteStores a single character/letter/number, or ASCII values

You will learn more about the individual data types in the next chapters.

Leave a Reply

Your email address will not be published.