Data Types

In C++ every variable must have a specified data type. The data type determines:

  • What values the variable can contain
  • How much memory the variable takes up
  • What operations we can perform with the variable

Basic data types in C++:

int number = 42;            // Whole numbers (-2147483648 to 2147483647)
double decimal = 3.14;      // Decimal numbers (precision ~15 decimal places)
float smallerDecimal = 3.14f;  // Decimal numbers (precision ~7 decimal places)
char character = 'A';       // One character (letter, number or symbol)
bool truth = true;          // Boolean value (true or false)
string text = "Hello";      // Text (requires #include <string>)

Examples of values for different types:

  • int: 42, -17, 0, 1234
  • double: 3.14, -0.001, 12345.6789
  • float: 3.14, -0.001, 12345.6
  • char: 'A', '1', '$', ' '
  • bool: true, false
  • string: "Hello", "12345", "C++"

Using the wrong type can lead to errors or data loss:

int x = 3.14;   // x will be 3 (decimal part is lost)
char y = "A";  // Error! For character use single quotes 'A'

Instructions

Declare and initialize the following variables with correct data types:

  • age with value 25 (whole number)
  • height with value 175.5 (decimal number)
  • letter with value 'A' (one character)
  • isStudent with value true (boolean value)

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/6

Data Types | Start Coder