Notes – Data Types in C++

A data type in C++ tells the compiler what kind of data a variable can hold.

Each variable in C++ must be declared with a specific type before use.


Why Are Data Types Important?

  • Help the compiler allocate memory correctly
  • Define what kind of operations can be performed on the variable
  • Prevent type-related errors during compilation

Categories of Data Types in C++


CategoryDescription
Primary (Built-in)Basic types like int, char, float, etc.
DerivedBased on built-in types, e.g., arrays, pointers, functions
User-definedCreated by the user, e.g., struct, class, enum
VoidRepresents absence of value, mostly used in functions

1. Primary (Built-in) Data Types


TypeSize (Typical)DescriptionExample
int4 bytesStores integersint age = 25;
char1 byteStores single characterchar grade = 'A';
float4 bytesStores decimal numbersfloat pi = 3.14;
double8 bytesMore precise decimaldouble area = 12.456;
bool1 byteStores true/falsebool isValid = true;

Sizes may vary slightly depending on compiler and system architecture.


2. Derived Data Types


TypeDescription
ArrayCollection of elements of same type
PointerStores memory address of a variable
FunctionFunction declaration or pointer to function
ReferenceAlias for another variable

3. User-defined Data Types


TypePurpose
structGroup of variables of different types
classObject-oriented data structure
enumDefines a set of named integer constants
unionShares memory among its members
typedefCreates an alias for existing data type

4. Void Data Type

  • Used when a function does not return a value.
void greet() {
cout << "Hello!" << endl;
}

Modifiers with Data Types


ModifierUsed WithPurpose
signedint, charCan store positive and negative values
unsignedint, charOnly positive values (increases range)
shortintSmaller integer size
longint, doubleLarger size and range

Example:

unsigned int x = 100;
long double bigValue = 123456.7890;