codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

7 / 87 topics
6Keywords and Identifiers7Variables, Literals, Constants & Storage Classes8Data Types & Type Modifiers9Type Conversion & Casting Operators10Operators11Booleans12Math & Numbers13Date and Time
Tutorials/C++ Programming/Variables, Literals, Constants & Storage Classes
⚡C++ Programming

Variables, Literals, Constants & Storage Classes

Updated 2026-05-12
30 min read

Variables, Literals, Constants & Storage Classes

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.

Introduction

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:

  • Variables: How to declare and initialize them.
  • Literals: Different types of literal values in C++ (integers, floating-point numbers, characters, strings, and booleans).
  • Constants: Using const, constexpr, and auto for fixed values.
  • Storage Classes: Understanding the scope and lifetime of variables using keywords like register, static, extern, and mutable.

Core Content

Variables

Variables are named storage locations that hold data. They must be declared before use, specifying their type and name.

Declaration and Initialization

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 int age; // Declaration
6 age = 25; // Initialization
7
8 cout << "Age: " << age << endl;
9 return 0;
10}
Output

Literals

Literals are constant values that appear directly in the source code. C++ supports several types of literals:

  • Integer Literals: Whole numbers without a fractional component.
  • Floating-point Literals: Numbers with a fractional component.
  • Character Literals: Single characters enclosed in single quotes.
  • String Literals: A sequence of characters enclosed in double quotes.
  • Boolean Literals: true or false.

Integer Literals

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num = 10; // Decimal
6 int hexNum = 0xA; // Hexadecimal
7 int octalNum = 012; // Octal
8
9 cout << "Decimal: " << num << endl;
10 cout << "Hexadecimal: " << hexNum << endl;
11 cout << "Octal: " << octalNum << endl;
12 return 0;
13}
Output
Decimal: 10
Hexadecimal: 10
Octal: 10

Floating-point Literals

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 float f = 3.14f; // Single precision
6 double d = 3.14; // Double precision
7
8 cout << "Float: " << f << endl;
9 cout << "Double: " << d << endl;
10 return 0;
11}
Output
Float: 3.14
Double: 3.14

Character and String Literals

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 char ch = 'A';
6 string str = "Hello, World!";
7
8 cout << "Character: " << ch << endl;
9 cout << "String: " << str << endl;
10 return 0;
11}
Output
Character: A
String: Hello, World!

Boolean Literals

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 bool isTrue = true;
6 bool isFalse = false;
7
8 cout << "Is True: " << isTrue << endl;
9 cout << "Is False: " << isFalse << endl;
10 return 0;
11}
Output
Is True: 1
Is False: 0

Constants

Constants are variables whose values cannot be changed after initialization. C++ provides several ways to define constants:

  • const: The value of a const variable cannot be modified.
  • constexpr: A constant expression evaluated at compile time.
  • auto: Automatically deduces the type from the initializer.

const

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 const int MAX_VALUE = 100;
6 // MAX_VALUE = 200; // Error: assignment of read-only variable 'MAX_VALUE'
7
8 cout << "Max Value: " << MAX_VALUE << endl;
9 return 0;
10}
Output

auto

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 auto num = 10; // int
6 auto pi = 3.14; // double
7
8 cout << "Number: " << num << endl;
9 cout << "Pi: " << pi << endl;
10 return 0;
11}
Output
Number: 10
Pi: 3.14

Storage Classes

Storage classes define the scope, visibility, and lifetime of variables. C++ supports several storage classes:

  • register: Suggests that a variable should be stored in a CPU register for faster access.
  • static: Limits the scope of a variable to its file or function block.
  • extern: Declares a global variable that is defined elsewhere.
  • mutable: Allows a member of a const object to be modified.

register

C++
1#include <iostream>
2using namespace std;
3
4int main() {
5 register int i;
6 for (i = 0; i < 10; i++) {
7 cout << i << " ";
8 }
9 return 0;
10}
Output

extern

C++
1// file1.cpp
2#include <iostream>
3using namespace std;
4
5extern int globalVar = 10; // Declaration of a global variable defined elsewhere
6
7void printGlobal() {
8 cout << "Global Var: " << globalVar << endl;
9}
10
11// file2.cpp
12#include <iostream>
13using namespace std;
14
15int globalVar = 10; // Definition of the global variable
16
17int main() {
18 printGlobal();
19 return 0;
20}
Output

Practical Example

Let's create a simple program that demonstrates the use of variables, literals, constants, and storage classes.

C++
1#include <iostream>
2using namespace std;
3
4extern int globalVar; // Declaration of an external variable
5
6int main() {
7 register int i;
8 static int count = 0;
9
10 for (i = 0; i < 5; i++) {
11 cout << "Iteration: " << i << ", Count: " << ++count << endl;
12 }
13
14 const int MAX_VALUE = 100;
15 constexpr int squareOfMax = MAX_VALUE * MAX_VALUE;
16
17 auto pi = 3.14f;
18 mutable int b = 20;
19
20 cout << "Global Var: " << globalVar << endl;
21 cout << "Square of Max Value: " << squareOfMax << endl;
22 cout << "Pi: " << pi << endl;
23 cout << "Mutable b: " << b << endl;
24
25 return 0;
26}
Output
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

Summary

ConceptDescription
VariablesNamed storage locations for data.
LiteralsConstant values appearing directly in the source code.
ConstantsFixed values that cannot be modified after initialization.
Storage ClassesDefine scope, visibility, and lifetime of variables.

What's Next?

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!


PreviousKeywords and IdentifiersNext Data Types & Type Modifiers

Recommended Gear

Keywords and IdentifiersData Types & Type Modifiers