×
>
<

C++ Programming

C++ Datatypes | CrackEase

Data Types in C++

How Data Types Work in C++

On this page we discuss the different data types in C++. When writing code we use variables to store information — for example:

  • Numbers / integers / decimals
  • Characters
  • Boolean values
  • Objects and compound types

What are data types?

Data types tell the compiler what kind of data a variable can store (e.g., character, integer, decimal, etc.).

Broad categories of data types in C++:

  • Primary (built-in / fundamental)
  • Derived (arrays, pointers, references)
  • User-defined (classes, structs, enums, unions)

Data Type Modifiers

Modifiers adjust the range or storage interpretation of primitive types; common modifiers are:

  • unsigned
  • signed
  • short
  • long

C++ primitive datatypes diagram
Initialization of Primary Data types

Below is a list of common primitive data types and their general use:

Data TypeShort NameUsage / Stored Values
Boolean bool Logical values: true / false
Character char Single characters (usually 1 byte)
Integer int Whole numbers
Floating point float Single-precision decimal numbers
Double floating point double Double-precision decimal numbers
void void Empty / no value (used with functions and pointers)
Wide character wchar_t Wide-character type for larger character sets

Below are typical sizes and ranges seen on common platforms. Note: exact sizes and ranges depend on the compiler and target architecture (e.g., 32-bit vs 64-bit).

Data Types — Typical Size and Range
Data Type Typical Size Typical Range / Notes
bool 1 byte true / false
char 1 byte typically -128 to 127 (signed) or 0 to 255 (unsigned)
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
short int 2 bytes -32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
int 4 bytes (typical on modern systems) typically −2,147,483,648 to 2,147,483,647 on 32/64-bit systems
unsigned int 4 bytes (typical) typically 0 to 4,294,967,295
long int 4 bytes (Windows) or 8 bytes (Linux x86_64) platform-dependent; ranges follow size
unsigned long int 4 or 8 bytes platform-dependent
float 4 bytes approx range: 1.17549e-38 to 3.40282e+38
double 8 bytes approx range: 2.22507e-308 to 1.79769e+308
long double 8, 12, or 16 bytes (implementation-defined) extended precision — platform-dependent
wchar_t 2 or 4 bytes size varies by platform (used for wide characters)
C++ Code to Print the size of datatypes :

The following code prints the size (in bytes) of common built-in types on the target platform:

  
#include <iostream>
using namespace std;

int main() 
{ 
    cout << "Size of char : " << sizeof(char) << " byte(s)" << endl; 
    cout << "Size of int : " << sizeof(int) << " byte(s)" << endl; 
    cout << "Size of short int : " << sizeof(short int) << " byte(s)" << endl; 
    cout << "Size of long int : " << sizeof(long int) << " byte(s)" << endl; 
    cout << "Size of signed long int : " << sizeof(signed long int) << " byte(s)" << endl; 
    cout << "Size of unsigned long int : " << sizeof(unsigned long int) << " byte(s)" << endl; 
    cout << "Size of float : " << sizeof(float) << " byte(s)" << endl; 
    cout << "Size of double : " << sizeof(double) << " byte(s)" << endl; 
    cout << "Size of wchar_t : " << sizeof(wchar_t) << " byte(s)" << endl; 

    return 0; 
}
  
Typical output (on a 64-bit system)
  
Size of char : 1 byte(s)
Size of int : 4 byte(s)
Size of short int : 2 byte(s)
Size of long int : 8 byte(s)
Size of signed long int : 8 byte(s)
Size of unsigned long int : 8 byte(s)
Size of float : 4 byte(s)
Size of double : 8 byte(s)
Size of wchar_t : 4 byte(s)
  
Understanding Each Datatype

Integer (int)

  • Used to store whole numbers like: -200, 150, 6812, etc.
  • Typical range: -2,147,483,648 to 2,147,483,647 (for 4-byte int).
  • Typical size: 4 bytes on modern systems (some older systems used 2 bytes).

  
int age = 25;
  

Float and Double

Float and double are used to store floating-point numbers (decimals and exponentials).

  • Size of float is typically 4 bytes.
  • Size of double is typically 8 bytes (more precision than float).
  • Use double when more precision is required.

  
Datatype                       Typical Range / Macro
float                          ~1.17549e-38 to 3.40282e+38        (FLT_MIN / FLT_MAX)
double                         ~2.22507e-308 to 1.79769e+308     (DBL_MIN / DBL_MAX)
  

Example :

  
float val1 = 21.25f;
double val2 = 1531.24595;
double val3 = 21.34E14;    // 21.34 × 10^14
double val4 = 1.23E-12;    // 1.23 × 10^-12    
  

Char

  • Size is typically 1 byte.
  • Characters are enclosed inside single quotes ' '.

  
char c = 'a';
  

Note: char val = '5'; stores the character '5' (its ASCII code), not the integer 5.

Bool

  • Typically 1 byte in size.
  • Used to store true / false values.
  • Commonly used in conditional operations and branching.

  
bool val = false;
  

wchar_t

  • wchar_t is a wide character type used for larger character sets (Unicode).
  • Typical size is 2 or 4 bytes depending on platform/compiler.
  • Useful when handling non-ASCII character sets.

  
wchar_t wideChar = L'あ'; // wide (Unicode) character literal
  

Void

  • void is used to indicate absence of type (for functions that return nothing, or pointer-to-void).
  • You cannot declare ordinary variables of type void.

Footer Content | CrackEase