✍️ C++ Basic Syntax & Language Elements
Estimated reading: 3 minutes 285 views

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:

CategoryIncludes
Primary/Built-inint, float, char, bool, double, void
DerivedArrays, Pointers, Functions, References
User-definedstruct, class, enum, union, typedef
Modifier Typesshort, long, signed, unsigned

1. Built-in Data Types

TypeSize*Range (Approx.)Example
int4 bytesβˆ’2,147,483,648 to 2,147,483,647int x = 100;
float4 bytes~Β±3.4eβˆ’38 to Β±3.4e+38float pi = 3.14;
double8 bytes~Β±1.7eβˆ’308 to Β±1.7e+308double g = 9.8;
char1 byteβˆ’128 to 127 or 0 to 255char grade = 'A';
bool1 bytetrue or falsebool isOpen = true;
void0 bytesRepresents no valuevoid 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

TypeDescription
ArraysCollection of similar data types (int arr[5];)
PointersStores address of another variable (int* ptr)
FunctionsReturn a value type (int sum(int, int);)
ReferencesAlias 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 :
Share

C++ Data Types

Or Copy Link

CONTENTS
Scroll to Top