C++ Variables & Data Types

Types, auto and constants.

✍️ 2 exercises in this lesson
  • int, long long: whole numbers
  • float, double: decimals
  • char: one character; bool: true/false
  • std::string: text (include <string>)
  • auto: let the compiler deduce the type
  • const and constexpr: constants

Brace initialisation int x{5}; prevents accidental narrowing conversions.

Key Takeaways: C++ Variables & Data Types

Mastering C++ Variables & Data Types helps you write clean and reliable C++ code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C++ Variables & Data Types Code Example

C++
#include <iostream>
#include <string>

int main() {
    int students{30};
    double fee = 15000.50;
    char section = 'B';
    bool open = true;
    std::string course = "C++ Basics";
    auto rating = 4.8;
    constexpr int MAX = 40;

    std::cout << course << " | section " << section << "\n";
    std::cout << students << "/" << MAX << " seats, open: " << std::boolalpha << open << "\n";
    std::cout << "Fee: " << fee << ", rating: " << rating << "\n";
    return 0;
}
Output
C++ Basics | section B
30/40 seats, open: true
Fee: 15000.5, rating: 4.8
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C++ Variables & Data Types Practice Exercises

Check what you learned in “C++ Variables & Data Types”.

/ 2 solved
Fill in the blank

Let the compiler deduce the type.

 rating = 4.8;
✓ Correct! Not quite. Try again.
Multiple choice

Which header gives you std::string?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services