Variables and types
Built-in types, auto, const, std::string, conversions, and integer division.
Runs in Zerethon IDE · C++ (GCC 14)
Official C++ documentation ↗
main.cpp
#include <iostream>
#include <string>
int main() {
int count = 3;
double price = 9.5;
long long big = 3'000'000'000LL;
bool ready = true;
std::string name = "Ada";
auto total = count * price; // double
constexpr int MAX = 10;
std::cout << count << ' ' << price << ' ' << big << ' ' << std::boolalpha << ready << ' ' << name << '\n';
std::cout << total << ' ' << MAX << '\n';
std::cout << 7 / 2 << ' ' << 7 / 2.0 << ' ' << 7 % 2 << ' ' << static_cast<int>(3.99) << '\n';
std::cout << std::stoi("42") + 1 << ' ' << std::stod("2.5") * 2 << ' ' << std::to_string(7) + "7" << '\n';
}
Output
3 9.5 3000000000 true Ada 28.5 10 3 3.5 1 3 43 5 77
int / intis whole-number division; make one sidedoublefor a fraction.- Use
std::stringfor text, notchar*; compare with==. - Prefer
static_cast<int>(x)to C-style(int) x. - A local variable without a value holds garbage — initialise it (
int n = 0;orint n{};).