Data Types in C++: Fundamental and Derived Types with Worked Examples

Learn how C++ fundamental types differ from arrays, pointers, references, and function types. Trace one complete program, then test the conversion traps.

KnowledgeGate Team

Exam prep & CS education

Updated 11 Nov 20265 min read
Data Types in C++: Fundamental and Derived Types with Worked Examples

int, double, and bool look simple until a question mixes them with an array, pointer, reference, function, or conversion and asks what the code prints.

Fundamental and derived/compound types differ in how declarations and expressions behave.

What a data type tells the C++ compiler

A type determines which values an object can represent, which operations are valid, and how the compiler interprets an expression.

cpp
int attempts = 3;
double accuracy = 87.5;
char section = 'A';
bool qualified = true;

Each declaration gives a name a type and initial value. In later operations, attempts + 2 produces integer 5, while accuracy / 2.0 produces floating-point 43.75.

C++ does not give int, long, or pointers one universal byte count. Check the implementation and the question's assumptions. Explore related topics through the Coding & Skills category.

Fundamental types: the built-in base categories

C++ fundamental types can be organised into these groups:

  • bool

  • character types

  • signed and unsigned integer types

  • floating-point types

  • void

  • std::nullptr_t

Here are concrete declarations:

cpp
bool ready = false;
char grade = 'B';
unsigned int retries = 4;
double ratio = 7.5;
std::nullptr_t empty = nullptr;

Integer types model whole values, floating-point types model fractional values, and bool represents true or false. void represents no value in contexts such as a function's return type. nullptr is the null pointer literal, with type std::nullptr_t.

Plain char, signed char, and unsigned char are distinct types. std::string is not fundamental; it is a standard-library class type. For bank-style classification, conversion, pointer, and reference problems, use C++ Data Types MCQs: 12 Fundamental and Derived Type Questions Solved. That companion owns question practice; this explanation owns the taxonomy and step-by-step traces.

Derived types, compound types, and exam terminology

Many exam notes call arrays, pointers, references, and function types derived types because they are formed from another type. From int, we can form:

cpp
int marks[4];
int* second;
int& third = marks[2];
int adjust(int score);

The C++ standard uses the broader term compound types. This family also includes class, union, enumeration, and pointer-to-member types. Many syllabi put classes, structures, unions, and enumerations under a separate user-defined heading.

Follow the terminology in the question while remembering the standard's wider classification. Here, "derived/compound" means arrays, pointers, references, and function types formed from another type.

Worked example: trace an array, pointer, reference, double, and bool

Array, pointer and reference trace changes 18 to 20, computes average 22.0, and prints 25 20 22 1.

Trace this program in source order:

cpp
#include <iostream>

int main() {
    int marks[4] = {12, 25, 18, 31};
    int* second = &marks[1];
    int& third = marks[2];

    third += 2;
    double average =
        (marks[0] + marks[1] + marks[2] + marks[3]) / 4.0;
    bool qualified = average >= 20.0;

    std::cout << *second << ' ' << marks[2] << ' '
              << average << ' ' << qualified;
    return 0;
}

The array begins as {12, 25, 18, 31}. second points to element 1, value 25. third aliases element 2, so third += 2 changes marks[2] from 18 to 20.

Now compute the average:

  1. Updated sum: 12 + 25 + 20 + 31 = 88.

  2. Division: 88 / 4.0 = 22.0 as a double.

  3. Comparison: 22.0 >= 20.0 is true.

Without std::boolalpha, the stream prints true as 1 and the average as 22. The exact output is:

Code
25 20 22 1

Here, int, double, and bool are fundamental. int[4], int*, and int& are derived/compound. The pointer stores an address; the reference aliases an object.

Trace of the four-element marks array showing cell 2 change from 18 to 20, the sum 88, average 22.0 and the printed output 25 20 22 1.

Conversions and declaration traps that change the answer

Types control when arithmetic and conversion happen. Consider:

cpp
int a = 5, b = 2;
double x = a / b;
double y = static_cast<double>(a) / b;
auto z = 5 / 2.0;

For x, integer division happens first: a / b is 2, then conversion makes x equal 2.0. For y, the cast happens before division, producing 2.5. The literal 2.0 makes z floating-point too, so auto deduces double and z is 2.5.

In int* p, q;, the * belongs to p, not every name. Only p is a pointer:

cpp
int value = 9;
p = &value;
q = 4;

Now *p is 9 and q is 4. Prefer one pointer declaration per line.

Keep three more distinctions ready:

  • An array is not a pointer, although an array converts to a pointer in many expressions.

  • A reference must be bound when declared and cannot later be reseated to another object.

  • const int limit = 10; is a cv-qualified integer type, not a new fundamental category.

How exams and interviews test C++ data types

Questions ask you to classify declarations, predict output after a mutation, evaluate mixed-type arithmetic, or spot an invalid declaration or conversion. Explain why the main program prints 20, why a / b is 2, and which of marks, second, and third is the array, pointer, or reference.

Try this short self-check:

  1. What is decltype(5 / 2.0)?

  2. After int n = 6; int& r = n; r = 8;, what do n and r contain?

  3. In int* p, q;, which name is a pointer?

  4. Is std::string name = "KG"; fundamental?

Answers: (1) double; (2) both read as 8; (3) only p; (4) no, it is a class type.

Once the ideas are clear, solve the companion set without rereading the explanations first.

Short version and the next practice step

  • Fundamental types are the built-in base categories.

  • Arrays, pointers, references, and function types are formed from other types.

  • Many notes call those forms derived, while the C++ standard uses compound more broadly.

  • Conversions can change an arithmetic result.

  • A reference aliases an existing object.

  • Implementation-dependent sizes must be checked, not memorised as universal facts.

For a 15-minute test, change the array to {10, 14, 22, 34}, point second to index 2, bind third to index 0, and apply third += 6. Predict the same four output fields before running the code.

The array becomes {16, 14, 22, 34}. Its sum is 16 + 14 + 22 + 34 = 86, so the average is 86 / 4.0 = 21.5 and the comparison is true. Index 0 now holds 16, while marks[2] is untouched at 22, so the program prints 22 22 21.5 1.

Continue with the complete C++ tutorial learning path. Practise foundational syntax and types in the C++ Programming course, or take the placement-focused Coding for Placements route.