Primitive Data Types
| Type | Size (bytes) | Range | Example |
|---|---|---|---|
| int | 4 | -2,147,483,648 to 2,147,483,647 | int age = 25; |
| float | 4 | ±3.4e±38 (~7 digits) | float pi = 3.14f; |
| double | 8 | ±1.7e±308 (~15 digits) | double precise = 3.1415926535; |
| char | 1 | -128 to 127 or 0 to 255 | char grade = 'A'; |
| bool | 1 | true or false | bool isValid = true; |
| void | N/A | N/A | void printMessage(); |
Type Modifiers
Size Modifiers
short int smallNum; // At least 2 bytes
long int bigNum; // At least 4 bytes
long long int hugeNum; // At least 8 bytesSign Modifiers
signed int normal; // Can be positive/negative (default)
unsigned int positive; // Only positive valuesDerived Data Types
Arrays
int numbers[5]; // Declaration
float temps[] = {36.5, 37.0}; // InitializationPointers
int num = 10;
int* ptr = # // Pointer to numReferences
int original = 5;
int& ref = original; // Reference to originalUser-Defined Types
Structures
struct Person {
string name;
int age;
float height;
};
Person p1;
p1.name = "Alice";Classes
class Rectangle {
private:
int width, height;
public:
void setDimensions(int w, int h) {
width = w;
height = h;
}
};Type Conversion
Implicit Conversion
int num = 10;
double d = num; // Automatically convertedExplicit Conversion
double pi = 3.14159;
int approx = (int)pi; // C-style cast
int precise = static_cast<int>(pi); // C++ castType Aliases
typedef unsigned long ulong; // Traditional
using Celsius = float; // Modern (C++11)
ulong bigNumber;
Celsius currentTemp = 23.5;