एक storage class identify करता है कि एक वह किस प्रकार का variable है और यह memory में कहां store होगा।
C ++ में storage class से हम एक variable का memory location , उनका scope और visibility आदि को specify करते हैं
चलिए इसे विस्तार से समझते हैं ,
Program में किसी भी task को करने के लिए, हम पहले variable declare करते हैं, जो अपने data type के अनुसार memory में space लेते हैं।
storage classes से हम निर्धारित कर सकते हैं कि कोई variable memory में कहाँ space लेगा और program कितनी देर तक visible देगा या active रहेगा।
Type of storage classes in C++
C ++ ने हमें 4 प्रकार के storage class या specifier प्रदान किए हैं जो इस प्रकार हैं-
- auto storage specifiers
- extern storage specifiers
- register storage specifiers
- static storage specifiers
auto in C++ hindi
function या block के भीतर declare होने वाले variable auto -variable कहलाते हैं।
ये variable program में तब तक रहते हैं जब तक कि function execution में रहता है (जिस function body में ये declared होते हैं )। जैसे ही function terminate होता है, वे स्वचालित रूप से destroy हो जाते हैं।
इस तरह आप कह सकते हैं कि सभी local variable default रूप से auto type variable ही होते हैं। इसलिए इसे default storage class भी कहा जाता है।
syntax
auto data-type variable-name;
इसमें auto
keyword का उपयोग optional होगा।
Example of auto-variable
नीचे program में declared सभी variable (i, f, s, name, lname) auto variable होंगे।
void main() //inside function
{
int i,f=0;
auto int s=0;
char name[]="Rahul";
auto char lname[]="sherma";
............
...........
}
इसका program नीचे दिया गया है –
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); // all local variable are auto variable
int i, f=3;
auto int s=5;
char name[]="Rahul";
auto char lname[]="sherma";
cout<<"\ninitial value: "<<i; // Garbage value will be print
cout<<"\nFirst value : "<<f;
cout<<"\nSecond value : "<<s;
cout<<"\nComplete Name: "<<name<<" "<<lname;
getch();
}
OUTPUT
initial value: -4314
First value : 3
Second value : 5
Complete name: Rahul sherma
Note✍: auto-variable stored in the stack memory location
extern in C++ in hindi
auto variables के विपरीत, वे variable जो किसी function या block के बाहर declare किए जाते हैं, extern variables कहलाते हैं।
अर्थात्, सभी global variable को extern variable होंगे। इन्हें program में ,किसी भी function या block द्वारा access किया जा सकता है। जिसे function body में अतिरिक्त variable declare नहीं करना पड़ता । जो इसका एक लाभ है।
क्योंकि सभी global variable extern variable हैं, इसलिए extern
keyword optional है।
syntax
extern data-type variable-name;
नीचे दिए गए program में declared सभी variable (i, f, name, lname) को extern variable कहा जाएगा-
extern int f=3;
int i;
char name[]="Rahul";
extern char lname[]="sherma";
void main()
{
...........
...........
}
Example of extern-variable
#include<iostream.h>
#include<conio.h>
//global variable
extern int f=3;
int i; // automaticaly intialized to zero
char name[]="Rahul";
extern char lname[]="sherma";
void main()
{
clrscr();
cout<<"intial value : "<<i; // print 0
cout<<"\nFirst value : "<<f;
cout<<"\nComplete Name: "<<name<<" "<<lname;
getch();
}
OUTPUT
initial value: 0
First value : 3
Name : Rahul sherma
Note✍: global variable stored in the data section or data segment memory.
register in C++ hindi
इन्हें auto variables की तरह local में declare किया जाता है, लेकिन जहां auto variable memory में store होते हैं वहीं register variable CPU register में store होते हैं। जिससे इन्हे अन्य variables की तुलना में जल्द ही access किया जा सकता है।
लेकिन अगर CPU register में space नहीं है तो इस स्थिति में , register variable auto variable की तरह memory में store होंगे।
एक variable को register type variable declare करने के लिए register
keyword का प्रयोग किया जाता है।
syntax
register data-type variable-name;
इसका program नीचे दिया गया है –
#include<iostream.h>
#include<conio.h>
void main()
{
register int a;
register int f=3; // register variable declartion
register char name[]="Rahul";
register int i; //only declare not intialized
clrscr();
cout<<"\ninitial value: "<<i;
cout<<"\nFirst value : "<<f; // Garbage value will be printed
cout<<"\nName : "<<name;
getch();
}
OUTPUT
initial value: 97234
First value : 3
Name : Rahul
static in C++
static variable को global और local दोनों में declare किया जा सकता है।
जब एक static variable को local में declare किया जाता है, तो इसे केवल function (जिसमें इसे declared होता है) body से ही access किया जा सकता है, लेकिन यह function terminate होने पर भी destroy नहीं होता है और program में visible रहता है।
global static variable का मान प्रत्येक function के लिए स्थिर होता है यानी प्रत्येक function execution में इनका मान नहीं बदलता।
एक static variable declare करने के लिए static
keyword का use किया जाता है।
syntax
static data-type variable-name;
Example of static-variable
चलिए इसे एक उदाहरण के साथ समझते हैं –
#include<iostream.h>
#include<conio.h>
//global declration of static variable
static s=3; // static variable initialzation
static i; // automatic initialized to zero
void main()
{
clrscr();
void first();
void second();
static int f=4; // local static variable
clrscr();
cout<<"\nFirst value : "<<i;
cout<<"\nSecond value : "<<s;
cout<<"\nlocal static variable: "<<f;
first();
second();
getch();
}
void first() // first function defination
{
cout<<"\nFirst function: "<<s;
/* cout<<"\nlocal static variable: "<<f; */ //can't execute
}
void second() // second function defination
{
cout<<"\nsecond function: "<<s;
}
OUTPUT
First value : 0
Second value : 3
local static variable: 4
First function: 3
second function:3
Note✍:– static variable storage location depends on their declaration(locally and globally).
previous – Passing arguments to function in C++ in hindi
next- string in C++ in hindi