Skip to main content

C++ Basics

Data Types & I/O

Complete lesson for Data Types & I/O using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with C++ OOP and STL where relevant.

L1

Basics — Data Types & I/O

Section: C++ Basics. Classification of C++ data types, size and range tables, bool, string, and I/O with cin / cout.

Classification of data types

  • A data type tells the compiler what kind of value a variable can hold and which operations are allowed.
  • C++ types are usually grouped as fundamental (built-in), derived, and user-defined.
  • Sizes below are typical on modern desktop compilers; always confirm with sizeof on your system.
CategoryExamplesWhat it stores
Fundamental — Integershort, int, long, long long (+ unsigned)Whole numbers
Fundamental — Floatingfloat, double, long doubleDecimal / real numbers
Fundamental — Characterchar, wchar_t, char16_t, char32_tSingle characters / code units
Fundamental — Booleanbooltrue / false
Fundamental — VoidvoidNo value (functions / generic pointers)
Derived — Pointerint*, char*Memory addresses
Derived — Arrayint a[5]Fixed sequence of same type
Derived — Referenceint&Alias to an existing object
User-defined — Stringstd::stringText (sequence of characters)
User-defined — StructurestructGroup of different members
User-defined — ClassclassData + member functions (OOP)
User-defined — Enum / Unionenum, unionNamed constants / shared storage

C++ Data Types Overview

Data types decide what a variable can store and which operations are safe. The diagram below groups the main families you will use: integers, floating-point numbers, characters, booleans, void, pointers, strings, arrays, structures, and classes.

C++ data types infographic covering integer, floating point, character, boolean, void, pointer, string, array, structure, and class types with examples and typical sizes.
C++ data types — choose the right type for safe data and efficient memory use
  • Integer family: short, int, long, long long.
  • Floating family: float, double, long double.
  • Simple built-ins: char, bool, and void.
  • Derived / library: pointers, arrays, std::string.
  • User-defined: struct and class for custom data models.

Integer types — size and range (typical)

  • Prefer int for everyday whole numbers unless you need a smaller or larger range.
  • Use long long for very large integers; use unsigned only when you truly need non-negative wrap semantics.
TypeTypical sizeApproximate rangeExample
short / short int2 bytes−32,768 to 32,767short marks = 85;
unsigned short2 bytes0 to 65,535unsigned short n = 40000;
int4 bytes≈ −2.1×10⁹ to 2.1×10⁹int age = 25;
unsigned int4 bytes0 to ≈ 4.2×10⁹unsigned int count = 0;
long4 or 8 bytesAt least as wide as intlong population = 1000000L;
long long8 bytes≈ −9×10¹⁸ to 9×10¹⁸long long big = 123LL;
unsigned long long8 bytes0 to ≈ 1.8×10¹⁹unsigned long long x = 1ULL;

Floating, char, bool, void — size and range (typical)

  • Use double as the default floating type for most calculations; use float when memory matters.
  • char stores one character; bool stores logical true/false; void means “no type/value”.
TypeTypical sizeRange / notesExample
float4 bytes≈ ±3.4×10³⁸ (about 6–7 digits precision)float pi = 3.14f;
double8 bytes≈ ±1.7×10³⁰⁸ (about 15 digits precision)double price = 99.99;
long double8 / 10 / 16 bytesImplementation-defined; often wider than doublelong double v = 3.14159L;
char1 byteUsually −128..127 or 0..255 (signedness varies)char grade = 'A';
unsigned char1 byte0 to 255unsigned char b = 255;
bool1 byte (typical)true or falsebool ok = true;
void—No objects of type void; used for no return / void*void show();

I/O with cin and cout

  • Use << for output and >> for input.
  • Prefer getline when you need a full line of text (including spaces).
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;
    cout << "Name: ";
    getline(cin, name);
    cout << "Age: ";
    cin >> age;
    cout << "Hi " << name << ", age " << age << endl;
}
L2

Advanced — Data Types & I/O

Deeper rules

  • Prefer modern C++ (C++11 and later) idioms over C-style patterns when both work.
  • Watch undefined behaviour: dangling references, out-of-bounds access, and use-after-move.
  • Use const, RAII, and the standard library as your default tools.
L3

Tricky Interview Points

Q1) What is a data type in C++?

Answer A data type defines what kind of value a variable can store and which operations are allowed on it (for example int for whole numbers, double for decimals).

Q2) How are C++ data types classified?

Answer Common groups are fundamental types (int, float, char, bool, void), derived types (pointer, array, reference), and user-defined types (struct, class, enum, std::string).

Q3) What is the typical size of int, float, double, and char?

Answer On most modern desktop compilers: int ≈ 4 bytes, float ≈ 4 bytes, double ≈ 8 bytes, char ≈ 1 byte. Always confirm with sizeof because sizes can vary by platform.

Q4) What is the difference between signed and unsigned integers?

Answer Signed types can store negative and positive values. Unsigned types store only non-negative values and usually have a larger positive maximum for the same size.

Q5) When should you use float vs double?

Answer Prefer double for most calculations because it has more precision. Use float mainly when memory or performance constraints matter and lower precision is acceptable.

Q6) Is bool a real type in C++?

Answer Yes. bool is a built-in type with values true and false (unlike older C habits of using int 0/1 for logic).

Q7) What does void mean?

Answer void means “no value.” A void function returns nothing. void* is a generic pointer type that can point to any object type (with casting rules).

Q8) Why prefer std::string over a char array for text?

Answer std::string manages length and memory more safely and conveniently. C-style char arrays need manual size handling and null terminators.

Q9) What is the difference between cin >> and getline?

Answer cin >> reads until whitespace (stops at spaces). getline reads a full line, including spaces, until newline.

Q10) How do you print the size of a type at runtime?

Answer Use the sizeof operator, for example cout << sizeof(int); or cout << sizeof(x); for a variable x.

L4

Simple Programs

Print sizeof basic types

#include <iostream>
using namespace std;

int main() {
    cout << "char: " << sizeof(char) << " bytes\n";
    cout << "int: " << sizeof(int) << " bytes\n";
    cout << "float: " << sizeof(float) << " bytes\n";
    cout << "double: " << sizeof(double) << " bytes\n";
    cout << "bool: " << sizeof(bool) << " bytes\n";
    return 0;
}

Declare variables of different types

#include <iostream>
#include <string>
using namespace std;

int main() {
    int age = 21;
    float height = 5.8f;
    double pi = 3.14159;
    char grade = 'A';
    bool passed = true;
    string name = "LearnCodeGuru";

    cout << name << " age=" << age
         << " height=" << height
         << " pi=" << pi
         << " grade=" << grade
         << " passed=" << boolalpha << passed << endl;
    return 0;
}

Simple I/O: read and display

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter two integers: ";
    cin >> a >> b;
    cout << "Sum = " << (a + b) << endl;
    return 0;
}

Read a full line with getline

#include <iostream>
#include <string>
using namespace std;

int main() {
    string line;
    cout << "Enter a full sentence: ";
    getline(cin, line);
    cout << "You typed: " << line << endl;
    return 0;
}
L5

Problem Solving Tricks

  • Always check type sizes on your machine with sizeof — never assume every compiler matches textbook sizes exactly.
  • Write floating literals with f for float (3.14f) and L for long double when needed.
  • Use long long (and LL suffix) for large whole numbers instead of overflowing int.
  • After cin >> x;, leftover newline can break the next getline — clear it with cin.ignore(...) when mixing both.
  • Print booleans as text with cout << boolalpha << flag;.
  • Prefer double for money/science demos unless the problem forces float.
  • For characters use single quotes ('A'); for text use double quotes ("Hello") with std::string or C-strings.
  • Include before using std::string; include for cin/cout.
L6

Practice Programs List

  1. Write a program that prints sizeof for short, int, long, long long, float, double, and bool.
  2. Declare one variable of each: int, float, double, char, bool, and string — then print all values.
  3. Read a student’s name (full line) and marks, then print a formatted result line.
  4. Take two floating values and print their sum, difference, and average using double.
  5. Read a character and print its ASCII code using an integer cast.
  6. Create a tiny “type quiz” program that asks the user to enter an int and a double, then reprints both with labels.
  7. Demonstrate the difference between cin >> word; and getline(cin, line); with sample inputs that contain spaces.
  8. Write a program that shows signed vs unsigned behavior by assigning a negative value to an unsigned int and printing the result.
  9. Store PI as float and double, print both, and compare the displayed precision.
  10. Build a mini form: name (string), age (int), height (float), isStudent (bool) — read all and print a summary.