کلاس های ذخیره سازی C برای توصیف ویژگی های یک متغیر/تابع استفاده می شود. این ویژگی ها اساساً شامل محدوده، دید و طول عمر می شود که به ما کمک می کند تا وجود یک متغیر خاص را در طول زمان اجرای یک برنامه ردیابی کنیم.
زبان C از 4 کلاس ذخیره سازی استفاده می کند که عبارتند از:
1. خودکار
این کلاس ذخیره سازی پیش فرض برای همه متغیرهای اعلام شده در داخل یک تابع یا یک بلوک است. از این رو، کلمه کلیدی auto به ندرت هنگام نوشتن برنامه ها به زبان C استفاده می شود. متغیرهای خودکار فقط در بلوک/عملکردی که اعلام شده اند قابل دسترسی هستند و نه خارج از آنها (که محدوده آنها را مشخص می کند). البته، اینها را می توان در بلوک های تو در تو در بلوک والد/عملکردی که در آن متغیر خودکار اعلام شده است، دسترسی داشت.
با این حال، آنها را می توان خارج از محدوده آنها و همچنین با استفاده از مفهوم اشاره گرهایی که در اینجا با اشاره به مکان بسیار دقیق حافظه که در آن متغیرها قرار دارند، دسترسی داشت. هر زمان که اعلام می شوند به طور پیش فرض یک مقدار زباله به آنها اختصاص داده می شود.
2. خارجی
کلاس ذخیره سازی خارجی به سادگی به ما می گوید که متغیر در جای دیگری تعریف شده است و نه در همان بلوکی که در آن استفاده می شود. اساساً، مقدار در یک بلوک دیگر به آن اختصاص داده می شود و می توان آن را در یک بلوک دیگر نیز بازنویسی/تغییر داد. بنابراین یک متغیر خارجی چیزی نیست جز یک متغیر سراسری که با مقدار قانونی مقداردهی اولیه شده است و در آنجا اعلام شده است تا در جای دیگری استفاده شود. می توان به آن در هر عملکرد / بلوک دسترسی داشت.
همچنین، یک متغیر جهانی معمولی را می توان با قرار دادن کلمه کلیدی «extern» قبل از اعلان/تعریف آن در هر تابع/بلوک، خارجی ساخت. این اساساً نشان میدهد که ما یک متغیر جدید را مقداردهی اولیه نمیکنیم، بلکه فقط از متغیر سراسری استفاده میکنیم/به آن دسترسی داریم. هدف اصلی استفاده از متغیرهای خارجی این است که بتوان به آنها بین دو فایل مختلف که بخشی از یک برنامه بزرگ هستند دسترسی داشت.
3. ایستا
این کلاس ذخیره سازی برای اعلام متغیرهای استاتیکی استفاده می شود که معمولاً هنگام نوشتن برنامه ها به زبان C مورد استفاده قرار می گیرند. متغیرهای استاتیک این خاصیت را دارند که ارزش خود را حتی پس از خارج شدن از محدوده خود حفظ کنند! از این رو، متغیرهای استاتیک ارزش آخرین استفاده خود را در محدوده خود حفظ می کنند. بنابراین می توان گفت که آنها فقط یک بار مقداردهی اولیه می شوند و تا پایان برنامه وجود دارند. بنابراین، هیچ حافظه جدیدی تخصیص داده نمی شود زیرا آنها دوباره اعلام نمی شوند.
دامنه آنها به تابعی که برای آن تعریف شده اند محلی است. متغیرهای استاتیک جهانی را می توان در هر نقطه از برنامه در دسترس قرار داد. به طور پیش فرض، مقدار 0 توسط کامپایلر به آنها اختصاص داده می شود.
4. ثبت نام کنید
این کلاس ذخیره سازی متغیرهای ثبتی را اعلام می کند که عملکردی مشابه با متغیرهای خودکار دارند. تنها تفاوت این است که کامپایلر سعی می کند در صورت وجود یک ثبات رایگان، این متغیرها را در ثبات ریزپردازنده ذخیره کند. این باعث می شود که استفاده از متغیرهای ثبت بسیار سریعتر از متغیرهای ذخیره شده در حافظه در طول زمان اجرای برنامه باشد.
اگر ثبت نام رایگان در دسترس نباشد، این موارد فقط در حافظه ذخیره می شوند. معمولاً تعداد کمی از متغیرهایی که در یک برنامه باید به طور مکرر به آنها دسترسی داشته باشید با کلمه کلیدی ثبت اعلام می شوند که زمان اجرای برنامه را بهبود می بخشد. نکته مهم و جالبی که در اینجا باید به آن اشاره کرد این است که نمی توانیم آدرس یک متغیر ثبات را با استفاده از اشاره گرها بدست آوریم.
نحو
برای تعیین کلاس ذخیره سازی برای یک متغیر، دستور زیر باید دنبال شود:
storage_class var_data_type var_name;
مثال
توابع از همان نحوی که در بالا برای متغیرها ارائه شد پیروی می کنند. برای توضیح بیشتر به مثال C زیر نگاه کنید:
[pmpro_signup submit_button="ثبت نام و خرید اشتراک یک ماهه" level="1" login="0" redirect="referrer" title="برای دیدن ادامه مطلب عضو vip سایت شوید" hidelabels="true" short="true" ][membership]- سی
C
// A C program to demonstrate different storage
// classes
#include <stdio.h>
// declaring the variable which is to be made extern
// an initial value can also be initialized to x
int x;
void autoStorageClass()
{
printf("\nDemonstrating auto class\n\n");
// declaring an auto variable (simply
// writing "int a=32;" works as well)
auto int a = 32;
// printing the auto variable 'a'
printf("Value of the variable 'a'"
" declared as auto: %d\n",
a);
printf("--------------------------------");
}
void registerStorageClass()
{
printf("\nDemonstrating register class\n\n");
// declaring a register variable
register char b = 'G';
// printing the register variable 'b'
printf("Value of the variable 'b'"
" declared as register: %d\n",
b);
printf("--------------------------------");
}
void externStorageClass()
{
printf("\nDemonstrating extern class\n\n");
// telling the compiler that the variable
// x is an extern variable and has been
// defined elsewhere (above the main
// function)
extern int x;
// printing the extern variables 'x'
printf("Value of the variable 'x'"
" declared as extern: %d\n",
x);
// value of extern variable x modified
x = 2;
// printing the modified values of
// extern variables 'x'
printf("Modified value of the variable 'x'"
" declared as extern: %d\n",
x);
printf("--------------------------------");
}
void staticStorageClass()
{
int i = 0;
printf("\nDemonstrating static class\n\n");
// using a static variable 'y'
printf("Declaring 'y' as static inside the loop.\n"
"But this declaration will occur only"
" once as 'y' is static.\n"
"If not, then every time the value of 'y' "
"will be the declared value 5"
" as in the case of variable 'p'\n");
printf("\nLoop started:\n");
for (i = 1; i < 5; i++) {
// Declaring the static variable 'y'
static int y = 5;
// Declare a non-static variable 'p'
int p = 10;
// Incrementing the value of y and p by 1
y++;
p++;
// printing value of y at each iteration
printf("\nThe value of 'y', "
"declared as static, in %d "
"iteration is %d\n",
i, y);
// printing value of p at each iteration
printf("The value of non-static variable 'p', "
"in %d iteration is %d\n",
i, p);
}
printf("\nLoop ended:\n");
printf("--------------------------------");
}
int main()
{
printf("A program to demonstrate"
" Storage Classes in C\n\n");
// To demonstrate auto Storage Class
autoStorageClass();
// To demonstrate register Storage Class
registerStorageClass();
// To demonstrate extern Storage Class
externStorageClass();
// To demonstrate static Storage Class
staticStorageClass();
// exiting
printf("\n\nStorage Classes demonstrated");
return 0;
}
// This code is improved by RishabhPrabhu
|
خروجی
A program to demonstrate Storage Classes in C Demonstrating auto class Value of the variable 'a' declared as auto: 32 -------------------------------- Demonstrating register class Value of the variable 'b' declared as register: 71 -------------------------------- Demonstrating extern class Value of the variable 'x' declared as extern: 0 Modified value of the variable 'x' declared as extern: 2 -------------------------------- Demonstrating static class Declaring 'y' as static inside the loop. But this declaration will occur only once as 'y' is static. If not, then every time the value of 'y' will be the declared value 5 as in the case of variable 'p' Loop started: The value of 'y', declared as static, in 1 iteration is 6 The value of non-static variable 'p', in 1 iteration is 11 The value of 'y', declared as static, in 2 iteration is 7 The value of non-static variable 'p', in 2 iteration is 11 The value of 'y', declared as static, in 3 iteration is 8 The value of non-static variable 'p', in 3 iteration is 11 The value of 'y', declared as static, in 4 iteration is 9 The value of non-static variable 'p', in 4 iteration is 11 Loop ended: --------------------------------
مقاله مرتبط:
در ادامه، متن انگلیسی این مطلب را میتوانید مشاهده نمایید:
C Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility, and lifetime which help us to trace the existence of a particular variable during the runtime of a program.
C language uses 4 storage classes, namely:
1. auto
This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto variable was declared.
However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables reside. They are assigned a garbage value by default whenever they are declared.
2. extern
Extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block.
Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead, we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
3. static
This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables have the property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared.
Their scope is local to the function to which they were defined. Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
4. register
This storage class declares register variables that have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program.
If a free registration is not available, these are then stored in the memory only. Usually, a few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers.
Syntax
To specify the storage class for a variable, the following syntax is to be followed:
storage_class var_data_type var_name;
Example
Functions follow the same syntax as given above for variables. Have a look at the following C example for further clarification:
- C
C
// A C program to demonstrate different storage
// classes
#include <stdio.h>
// declaring the variable which is to be made extern
// an initial value can also be initialized to x
int x;
void autoStorageClass()
{
printf("\nDemonstrating auto class\n\n");
// declaring an auto variable (simply
// writing "int a=32;" works as well)
auto int a = 32;
// printing the auto variable 'a'
printf("Value of the variable 'a'"
" declared as auto: %d\n",
a);
printf("--------------------------------");
}
void registerStorageClass()
{
printf("\nDemonstrating register class\n\n");
// declaring a register variable
register char b = 'G';
// printing the register variable 'b'
printf("Value of the variable 'b'"
" declared as register: %d\n",
b);
printf("--------------------------------");
}
void externStorageClass()
{
printf("\nDemonstrating extern class\n\n");
// telling the compiler that the variable
// x is an extern variable and has been
// defined elsewhere (above the main
// function)
extern int x;
// printing the extern variables 'x'
printf("Value of the variable 'x'"
" declared as extern: %d\n",
x);
// value of extern variable x modified
x = 2;
// printing the modified values of
// extern variables 'x'
printf("Modified value of the variable 'x'"
" declared as extern: %d\n",
x);
printf("--------------------------------");
}
void staticStorageClass()
{
int i = 0;
printf("\nDemonstrating static class\n\n");
// using a static variable 'y'
printf("Declaring 'y' as static inside the loop.\n"
"But this declaration will occur only"
" once as 'y' is static.\n"
"If not, then every time the value of 'y' "
"will be the declared value 5"
" as in the case of variable 'p'\n");
printf("\nLoop started:\n");
for (i = 1; i < 5; i++) {
// Declaring the static variable 'y'
static int y = 5;
// Declare a non-static variable 'p'
int p = 10;
// Incrementing the value of y and p by 1
y++;
p++;
// printing value of y at each iteration
printf("\nThe value of 'y', "
"declared as static, in %d "
"iteration is %d\n",
i, y);
// printing value of p at each iteration
printf("The value of non-static variable 'p', "
"in %d iteration is %d\n",
i, p);
}
printf("\nLoop ended:\n");
printf("--------------------------------");
}
int main()
{
printf("A program to demonstrate"
" Storage Classes in C\n\n");
// To demonstrate auto Storage Class
autoStorageClass();
// To demonstrate register Storage Class
registerStorageClass();
// To demonstrate extern Storage Class
externStorageClass();
// To demonstrate static Storage Class
staticStorageClass();
// exiting
printf("\n\nStorage Classes demonstrated");
return 0;
}
// This code is improved by RishabhPrabhu
|
Output
A program to demonstrate Storage Classes in C Demonstrating auto class Value of the variable 'a' declared as auto: 32 -------------------------------- Demonstrating register class Value of the variable 'b' declared as register: 71 -------------------------------- Demonstrating extern class Value of the variable 'x' declared as extern: 0 Modified value of the variable 'x' declared as extern: 2 -------------------------------- Demonstrating static class Declaring 'y' as static inside the loop. But this declaration will occur only once as 'y' is static. If not, then every time the value of 'y' will be the declared value 5 as in the case of variable 'p' Loop started: The value of 'y', declared as static, in 1 iteration is 6 The value of non-static variable 'p', in 1 iteration is 11 The value of 'y', declared as static, in 2 iteration is 7 The value of non-static variable 'p', in 2 iteration is 11 The value of 'y', declared as static, in 3 iteration is 8 The value of non-static variable 'p', in 3 iteration is 11 The value of 'y', declared as static, in 4 iteration is 9 The value of non-static variable 'p', in 4 iteration is 11 Loop ended: --------------------------------
Related Article:
[/membership]