C++ Data Types
In C++, data types define the type of data that can be stored in a variable. Understanding data types is essential for writing efficient and correct programs. C++ supports various types of data, including built-in (primitive) and user-defined types.
1. Built-in Data Types
These are the fundamental data types provided by C++:
Integer Types
Used to store whole numbers (positive or negative) without decimals.
-
int
: Used for general-purpose integers.cppint age = 25; // 4 bytes (on most systems)
-
short
: Used for shorter-range integers, typically smaller thanint
.cppshort x = 150; // 2 bytes
-
long
: Used for larger-range integers.cpplong population = 1000000; // 4 bytes (or 8 bytes on some systems)
-
long long
: Used for even larger integers.cpplong long distance = 9000000000LL; // 8 bytes
-
unsigned int
: Used for positive-only integers, which can store a larger positive range thanint
.cppunsigned int count = 300; // 4 bytes, no negative values
Floating-Point Types
Used for numbers with decimal points.
-
float
: Single precision, used for floating-point numbers with 7 digits of precision.cppfloat temperature = 36.5f; // 4 bytes
-
double
: Double precision, used for floating-point numbers with 15-16 digits of precision.cppdouble pi = 3.1415926535; // 8 bytes
-
long double
: Extended precision for floating-point numbers.cpplong double preciseValue = 3.141592653589793238L; // 10 bytes
Character Type
Used to store a single character or ASCII values.
-
char
: Stores a single character.cppchar grade = 'A'; // 1 byte
-
unsigned char
: Stores a non-negative character (ASCII values 0-255).cppunsigned char symbol = 255;
Boolean Type
Used to store true
or false
values.
-
bool
: Stores a boolean value (true
orfalse
).cppbool isAdult = true; // 1 byte
2. Derived Data Types
Derived from the built-in types, these include arrays, pointers, references, and functions.
-
Arrays: A collection of variables of the same type.
cppint numbers[5] = {1, 2, 3, 4, 5}; // Array of integers
-
Pointers: Stores the memory address of another variable.
cppint* ptr = &age; // Pointer to an integer variable
-
References: Acts as an alias for another variable.
cppint& ref = age; // Reference to the variable 'age'
3. User-Defined Data Types
C++ allows you to define your own data types, which include:
Structures (struct
)
Used to group variables of different data types under one name.
cppstruct Person {
std::string name;
int age;
float height;
};
Person john = {"John", 30, 5.9f}; // Creating a structure
Enumerations (enum
)
Used to define a set of named integer constants.
cppenum Color {Red, Green, Blue}; // Enum definition
Color myColor = Red; // Assigning an enum value
Classes
User-defined types that encapsulate both data and methods.
cppclass Car {
public:
std::string model;
int year;
};
Car myCar; // Creating an object of the Car class
myCar.model = "Tesla"; // Accessing class members
Typedef / Using
These are used to create type aliases.
cpptypedef unsigned int uint;
uint age = 25;
4. Void Type
The void
type is used to specify that a function does not return any value.
-
Example:
cppvoid printMessage() { std::cout << "Hello, World!" << std::endl; }
C++ Data Type Sizes
Data Type | Size (in bytes) | Range |
---|---|---|
char |
1 byte | -128 to 127 (signed), 0 to 255 (unsigned) |
int |
4 bytes | -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned) |
short |
2 bytes | -32,768 to 32,767 |
long |
4 bytes | -2,147,483,648 to 2,147,483,647 |
long long |
8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float |
4 bytes | ±3.4e−38 to ±3.4e+38 |
double |
8 bytes | ±1.7e−308 to ±1.7e+308 |
long double |
10 bytes | ±3.4e−4932 to ±3.4e+4932 |
bool |
1 byte | true or false |
Type Modifiers
Type modifiers allow you to adjust the size or behavior of a data type:
-
signed
: A modifier for integer types, allows storing both negative and positive values.cppsigned int x = -100;
-
unsigned
: Only stores positive values, effectively doubling the maximum range of positive values.cppunsigned int x = 200;
-
long
: Extends the size of integer or double types.cpplong int largeNumber = 1000000000;
Example Code with Various Data Types
cpp#include
#include
int main() {
int age = 25; // Integer
float height = 5.9f; // Float
char grade = 'A'; // Character
bool isStudent = true; // Boolean
std::string name = "Alice"; // String (using std::string)
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
std::cout << "Grade: " << grade << std::endl;
std::cout << "Is student: " << isStudent << std::endl;
return 0;
}
Output:
vbnetName: Alice
Age: 25
Height: 5.9
Grade: A
Is student: 1
Summary
- Primitive data types include integers, floating-point numbers, characters, and booleans.
- Derived data types include arrays, pointers, and references.
- User-defined types include structures, enumerations, and classes.
- Type modifiers like
signed
,unsigned
,long
are used to modify data types for specific purposes. - Understanding data types is critical for writing efficient and effective C++ programs.