Ad space (banner)
Lesson 3 / 20

Variables and Types (int/double/string/bool)

This lesson covers declaring variables using C++'s basic types โ€” int, double, string, and bool. It's for anyone searching "C++ variable type."

C++ is a statically typed language, meaning you have to specify a type when declaring a variable. The main types are int (integer), double (decimal), std::string (text), and bool (true/false).

The example declares two variables, std::string name = "Alice"; and int age = 14;, and outputs them chained together with cout.

A common early mistake is not realizing you need #include <string> to use std::string. Unlike C's string handling (a char array), C++'s std::string is a convenient type that makes resizing and concatenation easy.

In real projects, choosing the right type for the kind of data you're handling lets you fine-tune both memory usage and processing speed.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)