cpp / beginner
Snippet
Variables and Basic Data Types
Variables are containers for storing data values. In C++, every variable has a type that determines what kind of data it can hold. The four fundamental data types shown here are: int for whole numbers, double for decimal numbers, char for single characters, and bool for true/false values. Using descriptive variable names makes code more readable.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>int main() {int age = 25;double price = 19.99;char grade = 'A';bool isActive = true;std::cout << "Age: " << age << std::endl;std::cout << "Price: " << price << std::endl;std::cout << "Grade: " << grade << std::endl;std::cout << "Active: " << std::boolalpha << isActive << std::endl;return 0;}
Breakdown
1
int age = 25;
Integer variable storing whole number 25
2
double price = 19.99;
Double variable for decimal numbers with precision
3
char grade = 'A';
Character variable storing single ASCII character
4
bool isActive = true;
Boolean storing logical true value