Data Types(Primitive)
Data types give us a provision to store data in a more structured way.
In C++, primitive (or built-in) data types are the basic types that are predefined by the language and serve as the building blocks for data manipulation. These types are directly supported by the hardware and have a fixed size and behavior. Below are the primary primitive data types in C++:
1. Integer Types
int:Description: Represents a standard integer.
Size: Typically 4 bytes (32 bits) on most systems.
Range: -2,147,483,648 to 2,147,483,647.
short:Description: Represents a short integer, typically smaller than
int.Size: Typically 2 bytes (16 bits).
Range: -32,768 to 32,767.
long:Description: Represents a long integer, typically larger than
int.Size: Typically 4 or 8 bytes (32 or 64 bits).
Range: Varies by system, often -2,147,483,648 to 2,147,483,647 (if 4 bytes) or much larger if 8 bytes.
long long:Description: Represents an integer type that is larger than
long.Size: Typically 8 bytes (64 bits).
Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
unsigned:Description: Represents an integer type that cannot be negative.
Size: Same as the corresponding signed type.
Range: 0 to maximum positive value of the corresponding signed type (e.g.,
unsigned intis 0 to 4,294,967,295).
2. Floating-Point Types
float:Description: Represents a single-precision floating-point number.
Size: Typically 4 bytes (32 bits).
Range: Approximately 3.4e-38 to 3.4e+38 with 7 decimal digits of precision.
double:Description: Represents a double-precision floating-point number.
Size: Typically 8 bytes (64 bits).
Range: Approximately 1.7e-308 to 1.7e+308 with 15 decimal digits of precision.
long double:Description: Represents an extended-precision floating-point number.
Size: Typically 8, 10, or 16 bytes, depending on the system.
Range: Greater precision and range than
double, but varies by implementation.
3. Character Type
char:Description: Represents a single character (typically an ASCII character).
Size: 1 byte (8 bits).
Range: -128 to 127 (if
signed char), 0 to 255 (ifunsigned char).
wchar_t:Description: Represents a wide character, typically used for Unicode characters.
Size: 2 or 4 bytes, depending on the system.
Range: Varies depending on implementation, large enough to represent wide character sets.
char16_t(C++11):Description: Represents a UTF-16 character.
Size: 2 bytes (16 bits).
char32_t(C++11):Description: Represents a UTF-32 character.
Size: 4 bytes (32 bits).
4. Boolean Type
bool:Description: Represents a boolean value (
trueorfalse).Size: 1 byte (although it only needs 1 bit, it's typically stored in a byte for alignment reasons).
Values:
trueorfalse.
5. Void Type
void:Description: Represents the absence of type or value.
Usage: Commonly used to indicate that a function does not return a value or to declare a pointer to an unknown type (
void*).
Summary of C++ Primitive Data Types
Type
Description
Size
Range/Values
int
Standard integer
4 bytes
-2,147,483,648 to 2,147,483,647
short
Short integer
2 bytes
-32,768 to 32,767
long
Long integer
4 or 8 bytes
Varies, often the same as int or larger
long long
Larger integer
8 bytes
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned
Unsigned integer
Varies
0 to maximum positive value of the signed type
float
Single-precision floating-point
4 bytes
3.4e-38 to 3.4e+38
double
Double-precision floating-point
8 bytes
1.7e-308 to 1.7e+308
long double
Extended-precision floating-point
8, 10, or 16 bytes
Varies, greater than double
char
Character
1 byte
-128 to 127 or 0 to 255 (if unsigned)
wchar_t
Wide character
2 or 4 bytes
Varies
char16_t
UTF-16 character
2 bytes
0 to 65,535
char32_t
UTF-32 character
4 bytes
0 to 4,294,967,295
bool
Boolean value
1 byte
true or false
void
No type (used for functions that return nothing)
N/A
N/A
These primitive data types form the foundation of data handling in C++, allowing you to define variables, create complex data structures, and manipulate data efficiently.
Below is a simple C++ program that demonstrates the use of all the primitive data types:
Explanation:
Integer Types: Demonstrates
int,short,long,long long, andunsigned int.Floating-Point Types: Demonstrates
float,double, andlong double.Character Types: Demonstrates
char,wchar_t(wide character),char16_t(UTF-16), andchar32_t(UTF-32).Boolean Type: Demonstrates
bool.Void Type: Demonstrates the
voidtype by using avoid*pointer.
Output:
Notes:
wchar_t,char16_t, andchar32_tvalues may appear differently depending on your system's locale and character encoding support.boolprints1fortrueand0forfalse.void*is often used as a generic pointer, and its value here isnullptr, which is printed as0.
This program provides a comprehensive demonstration of the different primitive data types available in C++.
Last updated