cpp / beginner
Snippet
Understanding Variables and Data Types
This program demonstrates five fundamental C++ data types. Variables are containers that store values. int stores whole numbers, double stores decimal numbers, char stores single characters, bool stores true/false values, and std::string stores text sequences. The program declares variables, assigns values, and prints them to the console.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>#include <string>int main() {int age = 25;double price = 19.99;char grade = 'A';bool isActive = true;std::string name = "Max";std::cout << "Name: " << name << std::endl;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
#include <iostream>
Includes the input/output stream library for console output
2
#include <string>
Includes the string library for text handling
3
int main() {
Main function where program execution begins
4
int age = 25;
Integer variable storing the value 25
5
double price = 19.99;
Double variable storing a decimal number
6
char grade = 'A';
Character variable storing a single character
7
bool isActive = true;
Boolean variable storing true or false
8
std::string name = "Max";
String variable storing text
9
std::boolalpha << isActive
Outputs boolean as 'true'/'false' instead of 1/0