In this tutorial, we will explore the fundamental concepts of variables, literals, constants, and storage classes in C++. These elements are crucial for writing effective and efficient C++ programs. Understanding how to declare, initialize, and use these constructs will help you build robust applications.
Variables, literals, constants, and storage classes are the building blocks of any programming language. They allow you to store data and manipulate it during program execution. In this section, we will cover:
const, constexpr, and auto for fixed values.register, static, extern, and mutable.Variables are named storage locations that hold data. They must be declared before use, specifying their type and name.
1#include <iostream>2using namespace std;34int main() {5int age; // Declaration6age = 25; // Initialization78cout << "Age: " << age << endl;9return 0;10}
Literals are constant values that appear directly in the source code. C++ supports several types of literals:
true or false.1#include <iostream>2using namespace std;34int main() {5int num = 10; // Decimal6int hexNum = 0xA; // Hexadecimal7int octalNum = 012; // Octal89cout << "Decimal: " << num << endl;10cout << "Hexadecimal: " << hexNum << endl;11cout << "Octal: " << octalNum << endl;12return 0;13}
Decimal: 10 Hexadecimal: 10 Octal: 10
1#include <iostream>2using namespace std;34int main() {5float f = 3.14f; // Single precision6double d = 3.14; // Double precision78cout << "Float: " << f << endl;9cout << "Double: " << d << endl;10return 0;11}
Float: 3.14 Double: 3.14
1#include <iostream>2using namespace std;34int main() {5char ch = 'A';6string str = "Hello, World!";78cout << "Character: " << ch << endl;9cout << "String: " << str << endl;10return 0;11}
Character: A String: Hello, World!
1#include <iostream>2using namespace std;34int main() {5bool isTrue = true;6bool isFalse = false;78cout << "Is True: " << isTrue << endl;9cout << "Is False: " << isFalse << endl;10return 0;11}
Is True: 1 Is False: 0
Constants are variables whose values cannot be changed after initialization. C++ provides several ways to define constants:
const variable cannot be modified.1#include <iostream>2using namespace std;34int main() {5const int MAX_VALUE = 100;6// MAX_VALUE = 200; // Error: assignment of read-only variable 'MAX_VALUE'78cout << "Max Value: " << MAX_VALUE << endl;9return 0;10}
1#include <iostream>2using namespace std;34int main() {5auto num = 10; // int6auto pi = 3.14; // double78cout << "Number: " << num << endl;9cout << "Pi: " << pi << endl;10return 0;11}
Number: 10 Pi: 3.14
Storage classes define the scope, visibility, and lifetime of variables. C++ supports several storage classes:
const object to be modified.1#include <iostream>2using namespace std;34int main() {5register int i;6for (i = 0; i < 10; i++) {7cout << i << " ";8}9return 0;10}
1// file1.cpp2#include <iostream>3using namespace std;45extern int globalVar = 10; // Declaration of a global variable defined elsewhere67void printGlobal() {8cout << "Global Var: " << globalVar << endl;9}1011// file2.cpp12#include <iostream>13using namespace std;1415int globalVar = 10; // Definition of the global variable1617int main() {18printGlobal();19return 0;20}
Let's create a simple program that demonstrates the use of variables, literals, constants, and storage classes.
1#include <iostream>2using namespace std;34extern int globalVar; // Declaration of an external variable56int main() {7register int i;8static int count = 0;910for (i = 0; i < 5; i++) {11cout << "Iteration: " << i << ", Count: " << ++count << endl;12}1314const int MAX_VALUE = 100;15constexpr int squareOfMax = MAX_VALUE * MAX_VALUE;1617auto pi = 3.14f;18mutable int b = 20;1920cout << "Global Var: " << globalVar << endl;21cout << "Square of Max Value: " << squareOfMax << endl;22cout << "Pi: " << pi << endl;23cout << "Mutable b: " << b << endl;2425return 0;26}
Iteration: 0, Count: 1 Iteration: 1, Count: 2 Iteration: 2, Count: 3 Iteration: 3, Count: 4 Iteration: 4, Count: 5 Global Var: 10 Square of Max Value: 10000 Pi: 3.14 Mutable b: 20
| Concept | Description |
|---|---|
| Variables | Named storage locations for data. |
| Literals | Constant values appearing directly in the source code. |
| Constants | Fixed values that cannot be modified after initialization. |
| Storage Classes | Define scope, visibility, and lifetime of variables. |
In the next tutorial, we will delve into Data Types & Type Modifiers. Understanding data types is essential for managing memory and performing operations on different kinds of data. We will explore various data types available in C++ and how to modify them using type modifiers.
Stay tuned!