C++ Data Types

C++ is a typed language. That means, each variable is given a specific type which defines what values it can represent, how its data is stored in memory, and what operations can be performed on it.

So, a type of a variable is a formal description of what kind of data its value is. For instance,0 is an integer, 3.142 is a floating-point (decimal) number, and "Hello, world!\n" is a string value (a sequence of characters).

By forcing the programmer to explicitly define a type for all variables and interfaces, the type system enables the compiler to catch type-mismatch errors, thereby preventing a significant source of bugs.

 

Fundamental Data Types

Fundamental data types are basic types implemented directly by the language that represent the basic storage units supported natively by most systems. They can mainly be classified into:

  • Character types: They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte character. Other types are also provided for wider characters.
  • Numerical integer types: They can store a whole number value, such as 7 or 1024. They exist in a variety of sizes, and can either be signed or unsigned, depending on whether they support negative values or not.
  • Floating-point types: They can represent real values, such as 3.14 or 0.01, with different levels of precision, depending on which of the three floating-point types is used.
  • Boolean type: The boolean type, known in C++ as bool, can only represent one of two states, true or false.

The following table lists the data types and their storage sizes.

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
bool 1 byte Just true (1)or false (0).

Table : C++ data types and their sizes

Note I: Nearly all current machines represent an int with at least 32-bits and many now use 64-bits. The size of an int generally represents the natural word-size of a machine; the native size with which the CPU handles instructions and data.

We can use sizeof operator, returns an unsigned integer,  to find the size of a type in number of characters i.e. number of bytes. Do not be cofused that sizeof operator is not a function, although it often appears like one. It is a keyword.

Here is example program to find the size for different types:

/* program to print the size of various types in “number-of-bytes" */
#include <iostream >

using namespace std;

int main ()
{
    cout<<"Size for different types:\n";
    cout<<"void \t char \t short \t int \t long \t float \t double\n";
    cout<<sizeof(void)<<"\t"<<sizeof(char)<<"\t"<<sizeof(short)
        <<"\t"<<sizeof(int)<<"\t"<<sizeof(long)<<"\t"
        <<sizeof(float)<<"\t"<<sizeof(double)<<"\t";
    return 0;
}

Note: We can print a sequence of values by “chaining” the << symbol.

The ouput of the program is:

data types -sizes-c++

We have run the code in 64-bit system.

 

Introducing string

String class is an example of compund type. Variables of this type are able to store sequences of characters, such as words or sentences.

In order to declare and use objects (variables) of this type, the program needs to include the header where the type is defined within the standard library (header <string>).

//program to illstrate string class
#include <iostream>
#include <string>                            //includes string class file
using namespace std;

int main ()
{
  string my_string;                         //declares a string variable my_string
  my_string = "This is my first string.";   //initializes my_string
  cout << my_string << endl;                
  my_string = "This is another string.";    //assigns another string to my_string
  cout << my_string << endl;
  return 0;
}

The output of the program is:

This is my first string. 
This another string.

Note: endl inserts newline.

See more about string at the string class reference.

C++ Operators
C++ Variables and Expressions