C++ is a statically-typed, compiled programming language. In C++, there are several primitive data types that are used to store different types of values in the computer's memory.
Data Type | Size (in bytes) | Range |
short int | 2 | -32,768 to 32,767 |
int | 4 | -2,147,483,648 to 2,147,483,647 |
long int | 4 | -2,147,483,648 to 2,147,483,647 |
long long int | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 | ±3.40282347 x 10^38 |
double | 8 | ±1.7976931348623158 x 10^308 |
long double | 10 | ±1.18973149535723176502 x 10^4932 |
char | 1 | 0 to 255 |
bool | 1 | true or false |
Integer Data Types
short int
: Used to store small integers. It occupies 2 bytes in memory.int
: Used to store integers. It occupies 4 bytes in memory.long int
: Used to store larger integers. It occupies 4 bytes in memory.long long int
: Used to store very large integers. It occupies 8 bytes in memory.
Floating-Point Data Types
float
: Used to store floating-point numbers. It occupies 4 bytes in memory.double
: Used to store larger floating-point numbers. It occupies 8 bytes in memory.long double
: Used to store very large floating-point numbers. It occupies 10 bytes of memory.
Character Data Type
char
: Used to store a single character or ASCII value. It occupies 1 byte in memory.
Boolean Data Type
bool
: Used to store a boolean value, eithertrue
orfalse
. It occupies 1 byte in memory.
C++
#include <iostream>
using namespace std;
int main(){
// Integer
short int s = 10;
int i = 20;
long int l = 30;
long long int ll = 40;
// Float
float f = 1.5f;
double d = 2.5;
long double ld = 3.5;
// Character
char c = 'a';
// Boolean
bool b = true;
cout << "short int : " << s << endl;
cout << "int : " << i << endl;
cout << "long int : " << l << endl;
cout << "long long int : " << ll << endl;
cout << "float : " << f << endl;
cout << "double : " << d << endl;
cout << "long double : " << ld << endl;
cout << "char : " << c << endl;
cout << "bool : " << b << endl;
return 0;
}
Output
short int : 10
int : 20
long int : 30
long long int : 40
float : 1.5
double : 2.5
long double : 3.5
char : a
bool : 1
Conclusion:
These are the primitive data types in C++ along with code examples. It's important to choose the right data type for your application to store values efficiently in memory.