๐งฎ C++ Data Types โ Classifying Values in C++ Programs
๐งฒ Introduction โ Why Data Types Matter in C++
Every variable in C++ must be declared with a data type, which defines the kind of data it can hold and how much memory it consumes. Data types provide the blueprint for how values are stored and manipulated, making them crucial for writing efficient and type-safe code.
๐ฏ In this guide, youโll learn:
- The classification of C++ data types
- Size and range of basic types
- Syntax and usage examples
- Advanced and user-defined types
๐งพ What Are Data Types in C++?
A data type tells the compiler what kind of value a variable will store, such as integers, floating-point numbers, characters, or boolean values.
int age = 30;
float salary = 55000.50;
char grade = 'A';
๐งฉ Categories of Data Types
C++ data types are broadly categorized into:
๐๏ธ Category | Includes |
---|---|
Primary/Built-in | int , float , char , bool , double , void |
Derived | Arrays, Pointers, Functions, References |
User-defined | struct , class , enum , union , typedef |
Modifier Types | short , long , signed , unsigned |
๐ข 1. Built-in Data Types
Type | Size* | Range (Approx.) | Example |
---|---|---|---|
int | 4 bytes | โ2,147,483,648 to 2,147,483,647 | int x = 100; |
float | 4 bytes | ~ยฑ3.4eโ38 to ยฑ3.4e+38 | float pi = 3.14; |
double | 8 bytes | ~ยฑ1.7eโ308 to ยฑ1.7e+308 | double g = 9.8; |
char | 1 byte | โ128 to 127 or 0 to 255 | char grade = 'A'; |
bool | 1 byte | true or false | bool isOpen = true; |
void | 0 bytes | Represents no value | void show(); |
*Size may vary depending on architecture and compiler
โณ๏ธ 2. Modifier Data Types
These modify the behavior of base data types:
short int si;
long int li;
unsigned int ui;
โ Used to save memory or extend value ranges
๐ง 3. Derived Data Types
Type | Description |
---|---|
Arrays | Collection of similar data types (int arr[5]; ) |
Pointers | Stores address of another variable (int* ptr ) |
Functions | Return a value type (int sum(int, int); ) |
References | Alias to another variable (int &ref = a; ) |
๐งฑ 4. User-defined Data Types
Allow structuring custom data:
struct Student {
int id;
char name[30];
};
enum Color { RED, GREEN, BLUE };
Also includes:
class
union
typedef
enum class
(C++11)
๐ Size & Range with sizeof
#include <iostream>
using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes\n";
cout << "Size of float: " << sizeof(float) << " bytes\n";
return 0;
}
๐ Summary โ Recap & Next Steps
๐ Key Takeaways:
- C++ has built-in, derived, modifier, and user-defined data types
- Use appropriate data types to ensure type safety and memory efficiency
sizeof()
can help determine storage needs- Modifier types extend value ranges
โ๏ธ Real-World Relevance:
Choosing the right data type leads to optimized performance, lower memory usage, and more predictable code behavior in real-world applications.
โ FAQs โ C++ Data Types
โ What is the default data type for integers?
โ
int
is the default type for whole numbers.
โ Can I create my own data type?
โ
Yes. Use struct
, class
, union
, or typedef
.
โ What is the difference between float and double?
โ
double
has double the precision and size of float
.
โ Whatโs the use of void
type?
โ
Indicates that a function does not return a value.
โ Are data type sizes fixed across all systems?
โ No. They can vary by system and compiler.
Share Now :