C++ Data Types and Keywords
Fundamental Data Types
| Type | Size | Range | Use |
|---|---|---|---|
bool | 1 byte | true / false | Boolean logic |
char | 1 byte | -128 to 127 | Single character / ASCII |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 | Whole numbers |
long | 4"8 bytes | platform-dependent | Large integers |
long long | 8 bytes | +/-9.2 x 10^18 | Very large integers |
float | 4 bytes | ~+/-3.4 x 10^38 (6-7 digits) | Single-precision decimal |
double | 8 bytes | ~+/-1.7 x 10^308 (15-16 digits) | Double-precision decimal |
long double | 8"16 bytes | extended precision | High-precision decimal |
void | - | - | No value (function return) |
wchar_t | 2"4 bytes | wide characters | Unicode characters |
Type Modifiers
Modifiers adjust the range or sign of a base type:
| Modified Type | Size | Range |
|---|---|---|
unsigned int | 4 bytes | 0 to 4,294,967,295 |
unsigned char | 1 byte | 0 to 255 |
unsigned long long | 8 bytes | 0 to 18.4 x 10^18 |
short int | 2 bytes | -32,768 to 32,767 |
#include <iostream>
#include <climits> // INT_MAX, CHAR_MAX, etc.
#include <cfloat> // FLT_MAX, DBL_MAX
using namespace std;
int main() {
cout << "--- Sizes ---" << endl;
cout << "bool: " << sizeof(bool) << " byte" << endl;
cout << "char: " << sizeof(char) << " byte" << endl;
cout << "int: " << sizeof(int) << " bytes" << endl;
cout << "long long: " << sizeof(long long) << " bytes" << endl;
cout << "float: " << sizeof(float) << " bytes" << endl;
cout << "double: " << sizeof(double) << " bytes" << endl;
cout << "\n--- Limits ---" << endl;
cout << "INT_MAX: " << INT_MAX << endl;
cout << "INT_MIN: " << INT_MIN << endl;
cout << "CHAR_MAX: " << (int)CHAR_MAX << endl;
cout << "DBL_MAX: " << DBL_MAX << endl;
// C++11 fixed-width types (preferred in portable code)
#include <cstdint>
// int8_t, int16_t, int32_t, int64_t
// uint8_t, uint16_t, uint32_t, uint64_t
return 0;
}
C++ Keywords
Keywords are reserved words with special meaning. You cannot use them as variable names.
| Category | Keywords |
|---|---|
| Types | int, float, double, char, bool, void, auto, long, short, unsigned, signed |
| Control flow | if, else, switch, case, default, for, while, do, break, continue, return, goto |
| OOP | class, struct, public, private, protected, virtual, override, final, this, new, delete |
| Memory | new, delete, sizeof, nullptr |
| Templates | template, typename, concept, requires |
| Exceptions | try, catch, throw, noexcept |
| Modifiers | const, constexpr, static, extern, inline, volatile, mutable, explicit |
| Namespaces | namespace, using |
| Other | true, false, nullptr, operator, friend, typedef, enum, union |
#include <iostream>
using namespace std;
int main() {
// Implicit conversion (promotion)
int i = 5;
double d = i; // int â†' double automatically
cout << d << endl; // 5.0
// C-style cast (avoid in C++)
double pi = 3.14159;
int truncated = (int)pi; // 3
// C++ static_cast (preferred)
int a = 7, b = 2;
double result = static_cast<double>(a) / b; // 3.5
cout << "7/2 = " << result << endl;
// static_cast for char â†" int
char ch = 'A';
int ascii = static_cast<int>(ch);
cout << "'A' = " << ascii << endl; // 65
return 0;
}
Related C++ Topics