C++ Data Types

  • C++ provides built-in data types that correspond to integers, characters, floating-point values, and Boolean values.
  • These are the ways that data is commonly stored and manipulated by a program.
  • C++ allows you to construct more sophisticated types, such as classes, structures, and enumerations, but these too are ultimately composed of the built-in types.

At the core of the C++ type system are the seven basic data types shown here:

Type Meaning
char Character
wchar_t Wide character
int Integer
float Floating point
double Double floating point
bool Boolean
void Valueless

C++ allows certain of the basic types to have modifiers preceding them. A modifier alters the meaning of the base type so that it more precisely fits the needs of various situations. The data type modifiers are listed here:

  • signed
  • unsigned
  • long
  • short

Integers

Variables are the most fundamental part of any language. A variable has a symbolic name and can be given a variety of values. Variables are located in particular places in the computer’s memory.

int var1; 
int var2;

Characters

Variables of type char hold 8-bit ASCII characters such as A, z, or G, or any other 8-bit quantity. To specify a character, you must enclose it between single quotes.

Thus, this assigns X to the variable ch:

char ch;
ch = 'X';

cout << "This is ch: " << ch;

Output

This is ch: X

Floating-Point Types

  • Floating-point variables represent numbers with a decimal place—like 3.1415927, 0.0000625, and –10.2.
  • They have both an integer part, to the left of the decimal point, and a fractional part, to the right.
  • Floating-point variables represent what mathematicians call real numbers, which are used for measurable quantities such as distance, area, and temperature. They typically have a fractional part.

The bool Type

  • The bool type is a relatively recent addition to C++. It stores Boolean (that is, true/false) values.
  • C++ defines two Boolean constants, true and false, which are the only two values that a bool value can have. Before continuing, it is important to understand how true and false are defined by C++.
  • One of the fundamental concepts in C++ is that any nonzero value is interpreted as true and zero is false.

void

  • The void type specifies a valueless expression.