fbpx

How Variable Scope Impacts Your C++ Code?

How Variable Scope Impacts Your C++ Code


INTRODUCTION

Managing variable scope and storage effectively is a cornerstone of C++ programming. The scope of a variable determines its visibility and accessibility throughout the program. Variables in C++ are categorized based on their scope into local variables, which are confined to specific blocks or functions, and global variables, which remain accessible throughout the program.

When local and global variables share the same name, local variables take precedence, but global variables can still be accessed using the scope resolution operator (::).

In general, the scope is defined as the extent up to which something can be worked with.

There are two types of variables:

1. Local Variables

2. Global variables

1. Local variables are the variable that can be accessed with the function

Variables  which can be accessed within the { }

The cannot be accessed outside the block

#include<iostream.h>

using namespace std;

void func()

{

int age=18;

cout<<”age”<<age;

}

int main()

{

func();

 }

}

2. Global variables : Global variables  that can be accessed outside the function

They are available through out the life time of a program.

 

#include<iostream.h>

using namespace std;

int  empno =49; // global  variable

void func()

{

int age=18;

}

int main()

{

cout<<”age”<<age;

cout<<”empno”<<empno;

}

}

whenever we declare the local variables and global varaiable with same name:

#include<iostream.h>

using namespace std;

int  age=49; // global  variable

int main()

{

int age=12;

cout<<”age”<<age;

}

  • Local variables have a smaller scope (i.e., they are visible only inside the block or function where they are defined).
  • Global variables have a larger scope (i.e., they are visible throughout the program).
  • When a local variable has the same name as a global variable, the local variable “shadows” the global one within its scope. This means the local variable will be accessed because it is closer in scope to the point of use.

If you want to explicitly access the global variable, you can use the scope resolution operator (::):

#include<iostream.h>

using namespace std;

int  age=49; // global  variable

int main()

{

int age=12;

cout<<”local variable age”<<age;

cout<<”global variable age”<<::age;

}

Storage classes:

 

C++ Storage Classes are used to describe the characteristics of a variable/function.

 

It determines the lifetime, visibility, default value, and storage location which helps us to trace the existence of a particular variable during the runtime of a program. Storage class specifiers are used to specify the storage class for a variable.

C++ uses 6 storage classes, which are as follows:

auto Storage Class

register Storage Class

extern Storage Class

static Storage Class

mutable Storage Class

thread_local Storage Class

 

auto Storage Class:

————————

The auto storage class is the default for local variables. Since C++11, it is mostly used for type deduction.

 

#include<iostream.h>

using namespace std;

void func()

{

auto age=18;

cout<<”age”<<age;

}

int main()

{

func();

 }

register Storage Class:

——————————

Used for variables that need faster access. Modern compilers generally ignore this keyword.

#include<iostream.h>

using namespace std;

int  age=49; // global  variable

int main()

{

register int age=12;  // stores in the ram

cout<<”local variable age”<<age;

cout<<”global variable age”<<::age;

}

extern Storage Class:

—————————–

Declared the variable elsewhere that can be used in the another file  by using externl

If u are declaring as extern no file will be allocated.

File1:

——

#include <iostream>

using namespace std;

 

extern int num; // Declaration of external variable

void display();

 

int main() {

    display();

    cout << “num in file1: ” << num << endl;

    return 0;

}

File2

——

#include <iostream>

using namespace std;

int num = 42; // Definition of the variable

 

void display() {

    cout << “num in file2: ” << num << endl;

}

Static storage class:

The value is going to retained between the function calls

#include <iostream>

using namespace std;

 

void function()

{

static int count=0;

count++;

cout<<count<<endl;

}

int main() {

    function();

    function();

    return 0;

}

mutable Storage Class:

——————————

 

Allows a member of a const object to be modified.

 

// C++ program to illustrate the use of mutalbe storage

// class specifiers

#include <iostream>

using std::cout;

 

class Test {

public:

    int x;

 

    // defining mutable variable y

    // now this can be modified

    mutable int y;

 

    Test()

    {

        x = 4;

        y = 10;

    }

};

 

int main()

{

    // t1 is set to constant

    const Test t1;

 

    // trying to change the value

    t1.y = 20;

    cout << t1.y;

 

    // Uncommenting below lines

    // will throw error

    // t1.x = 8;

    // cout << t1.x;

    return 0;

}

thread_local Storage Class

#include <iostream>

#include <thread>

using namespace std;

 

thread_local int threadId = 0; // Thread-local variable

 

void printThreadId(int id) {

    threadId = id;

    cout << “Thread ID: ” << threadId << endl;

}

 

int main() {

    thread t1(printThreadId, 1);

    thread t2(printThreadId, 2);

 

    t1.join();

    t2.join();

 

    return 0;

}