Skip to content
Learn Netverks

Lesson

Step 7/36 19% through track

variables-types-cpp

Variables and types

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Variables and types: the syntax, patterns, and safety habits you need before advancing in C++.

Teams still ship Variables and types in C++ codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Variables and types in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

C++ variables have an explicit type: int count = 0;. Unlike JavaScript, you cannot silently change a variable from int to std::string without a compile error.

Common scalar types

  • int, long — integers
  • double, float — floating point (prefer double)
  • char — single character
  • booltrue or false

auto and initialization

auto count = 10;        // deduced as int
double ratio = 0.75;
bool ok = true;

Unlike C, C++ encourages initialization—value-initialize or assign before use.

Important interview questions and answers

  1. Q: Size of int?
    A: Implementation-defined but at least 16 bits; use <cstdint> for fixed widths when needed.
  2. Q: auto pitfalls?
    A: auto x = ref; copies unless you write auto& or const auto&.

Self-check

  1. What type does auto deduce from an integer literal?
  2. How is bool different from C truthiness?

Pitfall: auto without reference captures copies—use const auto& in range-for when iterating large containers.

Interview prep

Why initialize variables?

Although C++ improves on C, uninitialized built-ins have indeterminate values until assigned—always initialize.

bool vs C truthiness?

C++ has a distinct bool type with true and false keywords.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • size_t when?
  • bool vs int?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump