cpp / beginner
Snippet
Variables and Constants in C++
Variables store data that can change during program execution. Constants hold fixed values that cannot be modified after initialization. C++ requires you to declare the type of each variable before using it, making your code more predictable and faster.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>using namespace std;int main() {int age = 25;const string PROGRAMMING_LANGUAGE = "C++";float pi = 3.14159f;bool isLearning = true;cout << "Age: " << age << endl;cout << "Language: " << PROGRAMMING_LANGUAGE << endl;cout << "Pi: " << pi << endl;cout << "Learning: " << isLearning << endl;return 0;}
Breakdown
1
#include <iostream>
Includes the input/output stream library for cout and endl
2
using namespace std;
Allows use of standard library names without std:: prefix
3
int age = 25;
Declares an integer variable named age with value 25
4
const string PROGRAMMING_LANGUAGE = "C++";
Declares a constant that cannot be reassigned later
5
float pi = 3.14159f;
Declares a floating-point number with f suffix for float type
6
bool isLearning = true;
Declares a boolean that stores true or false values