Types, Variables, and I/O
Types, Variables, and I/O
Learn how data is stored, initialized, and printed in small C++ programs.
Types, Variables, and I/O
Learn how data is stored, initialized, and printed in small C++ programs.
int count = 3;
double price = 9.99;
bool ready = true;
char grade = 'A';
std::string name = "Ada";
Prefer brace initialization when practical:
int age{42};
double ratio{0.5};
std::cout << name << " has age " << age << '\n';
int year{};
std::cin >> year;
#include <iostream>
#include <string>
int main() {
std::string name;
int age{};
std::cout << "Name: ";
std::getline(std::cin, name);
std::cout << "Age: ";
std::cin >> age;
std::cout << name << " will be " << age + 1 << " next year.\n";
}
constconst auto max_retries = 3;
auto price = 19.99;
Use const by default for local values that should not change. Use auto when the initializer already makes the type obvious.
void greet(std::string_view name) {
std::cout << "Hello, " << name << '\n';
}
std::string_view avoids copies for read-only text parameters, but it does not own data. The caller must keep the underlying string alive.
If you mix std::cin >> value with std::getline, consume the leftover newline first or structure input as full lines and parse afterwards.