Data types in C++

A data type fixes how many bytes a value gets and what operations make sense on it, and C++ adds bool and references on top of the C types you already know.

9 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The 50 paise that vanished

The canteen till stores the chai price in an int:

int price = 12.50;

C++ quietly keeps 12 and drops the .50. Nobody notices, until the owner multiplies the loss: 50 paise on around 900 chai a day is Rs 450, gone daily, because of one wrong type choice.

A data type is not a formality. It decides what a variable can hold and what it silently throws away.

Theory

Containers behind the counter

The canteen never serves dal in a paper bag or chai in a thali. Each container has a shape, a capacity, and things it can safely hold.

Data types are the containers of a program: int is the steel tumbler for whole numbers, double the wide bowl for decimals, char the tiny sauce cup for one character, bool a two-position switch. Choose the wrong container and the content spills.

Theory

What a data type really decides

A data type tells the compiler three things about a variable:

  • Size: how many bytes to reserve (an int typically gets 4).
  • Interpretation: what the bits mean (the same bits could be a number or a character).
  • Operations: what is legal (you can divide ints, you cannot divide bools).

C++ groups types into three families: fundamental (built-in), derived (built from others), and user-defined (you create them).

At a glance

Fundamental types at a glance (typical sizes)

TypeSizeHolds
char1 byteOne character: 'A' (stored as ASCII code)
int4 bytesWhole numbers: 12, -900
float4 bytesDecimals, about 6-7 digits precision
double8 bytesDecimals, about 15 digits precision
bool1 bytetrue or false
void0No value (function returns nothing)

Theory

What C++ adds over the C you know

From BCA104 you know int, float, char and the modifiers short, long, signed, unsigned. C++ brings new pieces:

  • bool: a real true/false type (old C faked it with 0 and 1).
  • Reference (int& r = x;): a second name for an existing variable, heavily used in functions later.
  • wchar_t: wide characters for larger character sets.

Derived types: array, pointer, function, reference. User-defined types: class, struct, union, enum, and class is where this subject lives.

Think first

Predict the output

The till programmer writes:

char c = 65;

cout << c;

Take five seconds: what appears on screen, 65 or something else?

Show the answer

It prints A. A char stores a small number and interprets it as a character: 65 is the ASCII code for 'A'. Same bits, different meaning, because the type controls interpretation. If you wanted to see 65, you would store it in an int or cast: cout << (int)c;.

Quiz

The canteen needs to store the price 12.50 and later totals like 84756.75 without losing paise. Which type is the exam-safe choice?

  1. double
  2. int
  3. char
  4. bool
Show the answer

double

double holds decimals with about 15 digits of precision, comfortably exact for money-sized values. int truncates 12.50 to 12 (the vanished paise). char holds one character, bool only true/false. If you thought of float: it works, but its 6-7 digit precision starts wobbling on big totals, so double is the safer written answer.

Watch out

Two silent money-losers

Integer division: 5 / 2 is 2, not 2.5. Both operands are ints, so the result is an int. Write 5.0 / 2 to get 2.5.

Hardcoded sizes: never write "int is 4 bytes" as a universal fact in an exam. Sizes depend on the machine and compiler, so write "typically 4 bytes" and mention sizeof(int) as the way to check. Examiners specifically look for this word.

Formula

The classification tree exams ask you to draw

Data types

├── Fundamental: int, char, float, double, bool, void

├── Derived: array, pointer, function, reference

└── User-defined: class, struct, union, enum

A 5-mark "explain data types in C++" answer is this tree plus one line and one example per family. Practice drawing it once from memory.

Summary

Key takeaways

  • A data type decides size in bytes, interpretation of the bits, and legal operations.
  • Three families: fundamental (int, char, float, double, bool, void), derived (array, pointer, function, reference), user-defined (class, struct, union, enum).
  • C++ over C: a real bool, references (int&), wchar_t.
  • char stores an ASCII code: char c = 65 prints A.
  • Money needs double; int truncates and 5/2 is 2 (integer division).
  • Sizes are machine dependent: write "typically", check with sizeof.
  • Memory hook: right container for the right food.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of OOPS

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati