Monday, 22 December 2025

Storage Class Specifiers in C++

Storage Class Specifiers in C++
Storage class specifiers in C++ define the scope, lifetime, visibility, and storage location of variables and functions.

1. auto
  • Default storage class for local variables.
  • Automatically assigns storage; no need to specify.
  • Scope: local
  • Lifetime: within the block
2. register
  • Requests the compiler to store the variable in a CPU register for faster access.
  • Scope: local
  • Lifetime: within the block
  • The address of a register variable cannot be accessed using &.
Example:
void fun() 
{
    register int count = 0;
}

3. static
  • Preserves the value of a variable between function calls.
  • Scope depends on where it is declared.
  • Lifetime: entire program execution
(a) Static local variable
void fun() 
{
    static int x = 0;
    x++;
    cout << x << endl;
}
Output on successive calls: 1 2 3 ...

(b) Static global variable
Accessible only within the file in which it is declared.

static int total = 100;

4. external
  • Used to declare a global variable or function defined in another file.
  • Does not allocate memory; only refers to an existing variable.
  • Lifetime: entire program
Example:
File1.cpp
int x = 50;
File2.cpp
extern int x;
cout << x;

5. mutable
  • Allows a class data member to be modified even if the object is const.
  • Used only with class members.
Example:
class Test 
{
public:
    mutable int x;
};
int main() 
{
    const Test t;
    t.x = 10;   // allowed
}

Storage Class         Scope             Lifetime            Special Feature
auto                 Local             Block                     Default local storage
register                         Local             Block                     Faster access
static                         Local/Global     Entire program     Value retained
external                        Global             Entire program     Refers to external variable
mutable                         Class                     Object lifetime     Modifiable in const object

Recursive Function in C++
A recursive function is a function that calls itself to solve a problem by breaking it into smaller sub-problems.

Key Parts of a Recursive Function
  • Base Condition 
           Stops the recursion and prevents infinite calls.
  • Recursive Call  
            The function calls itself with a smaller or simpler input.

General Syntax
return_type function_name(parameters) 
{
    if (base_condition)
        return value;
    else
        return function_name(smaller_parameters);
}

Example 1: Factorial of a Number
Definition:
n! = n × (n−1)!, and 0! = 1

#include <iostream>
using namespace std;
int factorial(int n) 
{
    if (n == 0)
        return 1;
    else
        return n * factorial(n - 1);
}
int main() 
{
    int num = 5;
    cout << "Factorial = " << factorial(num);
    return 0;
}
Output:
Factorial = 120

Example 2: Fibonacci Series
#include <iostream>
using namespace std;
int fibonacci(int n) 
{
    if (n == 0) return 0;
    if (n == 1) return 1;
    return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() 
{
    int n = 6;
    cout << "Fibonacci = " << fibonacci(n);
    return 0;
}
Example 3: Sum of Natural Numbers
int sum(int n) 
{
    if (n == 0)
        return 0;
    else
        return n + sum(n - 1);
}

Advantages of Recursion
  • Code is short and simple
  • Suitable for problems like
  • Tree traversal
  • Factorial
  • Fibonacci
  • Tower of Hanoi
Disadvantages of Recursion
  • Uses more memory (function call stack)
  • Can be slower than loops
  • Risk of stack overflow if base condition is missing
Difference: Recursion vs Loop
Recursion                                 Loop
A function calls itself                  Repeats using loop
Uses stack memory                  Uses less memory
Easier for complex problems Faster execution

0 comments

Post a Comment