2026/4/18 1:15:38
网站建设
项目流程
腾讯云建站平台,wordpress怎么做的,遵义网帮你,如何做网站栏目存储类定义 C 程序中变量/函数的范围#xff08;可见性#xff09;和生命周期。这些说明符放置在它们所修饰的类型之前。下面列出 C 程序中可用的存储类#xff1a;auto#xff1a;这是默认的存储类说明符#xff0c;通常可以省略不写。auto 指定的变量具有自动存储期 程序中变量/函数的范围可见性和生命周期。这些说明符放置在它们所修饰的类型之前。下面列出 C 程序中可用的存储类auto这是默认的存储类说明符通常可以省略不写。auto 指定的变量具有自动存储期即它们的生命周期仅限于定义它们的块block。auto 变量通常在栈上分配。register用于建议编译器将变量存储在CPU寄存器中以提高访问速度。在 C11 及以后的版本中register 已经是一个废弃的特性不再具有实际作用。static用于定义具有静态存储期的变量或函数它们的生命周期贯穿整个程序的运行期。在函数内部static变量的值在函数调用之间保持不变。在文件内部或全局作用域static变量具有内部链接只能在定义它们的文件中访问。extern用于声明具有外部链接的变量或函数它们可以在多个文件之间共享。默认情况下全局变量和函数具有 extern 存储类。在一个文件中使用extern声明另一个文件中定义的全局变量或函数可以实现跨文件共享。mutable (C11)用于修饰类中的成员变量允许在const成员函数中修改这些变量的值。通常用于缓存或计数器等需要在const上下文中修改的数据。thread_local (C11)用于定义具有线程局部存储期的变量每个线程都有自己的独立副本。线程局部变量的生命周期与线程的生命周期相同。从 C 17 开始auto 关键字不再是 C 存储类说明符且 register 关键字被弃用。中的存储类说明符为程序员提供了控制变量和函数生命周期及可见性的手段。合理使用存储类说明符可以提高程序的可维护性和性能。从 C11 开始register 已经失去了原有的作用而 mutable 和 thread_local 则是新引入的特性用于解决特定的编程问题。下面是一个展示不同存储类说明符的实例实例#include iostream// 全局变量具有外部链接默认存储类为externint globalVar;void function() {// 局部变量具有自动存储期默认存储类为autoauto int localVar 10;// 静态变量具有静态存储期生命周期贯穿整个程序static int staticVar 20;const int constVar 30; // const变量默认具有static存储期// 尝试修改const变量编译错误// constVar 40;// mutable成员变量可以在const成员函数中修改class MyClass {public:mutable int mutableVar;void constMemberFunc() const {mutableVar 50; // 允许修改mutable成员变量}};// 线程局部变量每个线程有自己的独立副本thread_local int threadVar 60;}int main() {extern int externalVar; // 声明具有外部链接的变量function();return 0;}auto 存储类自 C 11 以来auto关键字用于两种情况声明变量时根据初始化表达式自动推断该变量的类型、声明函数时函数返回值的占位符。C98 标准中 auto 关键字用于自动变量的声明但由于使用极少且多余在 C17 中已删除这一用法。根据初始化表达式自动推断被声明的变量的类型如auto f3.14; //double auto s(hello); //const char* auto z new auto(9); // int* auto x1 5, x2 5.0, x3r;//错误必须是初始化为同一类型register 存储类register 是一种存储类storage class用于声明变量并提示编译器将这些变量存储在寄存器中以便快速访问。使用 register 关键字可以提高程序的执行速度因为它减少了对内存的访问次数。然而需要注意的是register 存储类只是一种提示编译器可以忽略它因为现代的编译器通常会自动优化代码选择合适的存储位置。语法格式register data_type variable_name;register是存储类的关键字用于提示编译器将变量存储在寄存器中。data_type是变量的数据类型可以是任何合法的 C 数据类型。variable_name是变量的名称。void loop() { register int i; for (i 0; i 1000; i) { // 循环体 } }register 存储类用于提示编译器将变量存储在寄存器中以便提高访问速度。然而由于现代编译器的自动优化能力使用 register 关键字并不是必需的而且在实践中很少使用。在 C11 标准中register 关键字不再是一个存储类说明符而是一个废弃的特性。这意味着在 C11 及以后的版本中使用 register 关键字将不会对程序产生任何影响。在 C 中可以使用引用或指针来提高访问速度尤其是在处理大型数据结构时。static 存储类static存储类指示编译器在程序的生命周期内保持局部变量的存在而不需要在每次它进入和离开作用域时进行创建和销毁。因此使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。static 修饰符也可以应用于全局变量。当 static 修饰全局变量时会使变量的作用域限制在声明它的文件内。在 C 中当 static 用在类数据成员上时会导致仅有一个该成员的副本被类的所有对象共享。实例#include iostream // 函数声明 void func(void); static int count 10; /* 全局变量 */ int main() { while(count--) { func(); } return 0; } // 函数定义 void func( void ) { static int i 5; // 局部静态变量 i; std::cout 变量 i 为 i ; std::cout , 变量 count 为 count std::endl; }当上面的代码被编译和执行时它会产生下列结果变量 i 为 6 , 变量 count 为 9 变量 i 为 7 , 变量 count 为 8 变量 i 为 8 , 变量 count 为 7 变量 i 为 9 , 变量 count 为 6 变量 i 为 10 , 变量 count 为 5 变量 i 为 11 , 变量 count 为 4 变量 i 为 12 , 变量 count 为 3 变量 i 为 13 , 变量 count 为 2 变量 i 为 14 , 变量 count 为 1 变量 i 为 15 , 变量 count 为 0extern 存储类extern存储类用于提供一个全局变量的引用全局变量对所有的程序文件都是可见的。当您使用 extern 时对于无法初始化的变量会把变量名指向一个之前定义过的存储位置。当您有多个文件且定义了一个可以在其他文件中使用的全局变量或函数时可以在其他文件中使用extern来得到已定义的变量或函数的引用。可以这么理解extern是用来在另一个文件中声明一个全局变量或函数。extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候如下所示第一个文件main.cpp实例#include iostream int count ; extern void write_extern(); int main() { count 5; write_extern(); }第二个文件support.cpp实例#include iostream extern int count; void write_extern(void) { std::cout Count is count std::endl; }在这里第二个文件中的extern关键字用于声明已经在第一个文件 main.cpp 中定义的 count。现在 编译这两个文件如下所示$ g main.cpp support.cpp -o write这会产生write可执行程序尝试执行write它会产生下列结果$ ./write Count is 5mutable 存储类mutable 是一个关键字用于修饰类的成员变量使其能够在 const 成员函数中被修改。通常情况下const 成员函数不能修改对象的状态但如果某个成员变量被声明为 mutable则可以在 const 函数中对其进行修改。特点允许修改mutable成员变量可以在const成员函数内被改变。设计目的通常用于需要在不改变对象外部状态的情况下进行状态管理的场景比如缓存、延迟计算等。实例#include iostreamclass Example {public:Example() : value(0), cachedValue(0) {}// 常量成员函数int getValue() const {return value; // 读取常量成员}// 修改 mutable 成员void increment() {value;cachedValue value * 2; // 修改 mutable 成员}int getCachedValue() const {return cachedValue; // 读取 mutable 成员}https://avg.163.com/topic/detail/8121231https://avg.163.com/topic/detail/8121250https://avg.163.com/topic/detail/8103525https://avg.163.com/topic/detail/8121276https://avg.163.com/topic/detail/8104256https://avg.163.com/topic/detail/8121300https://avg.163.com/topic/detail/8104887https://avg.163.com/topic/detail/8103532https://avg.163.com/topic/detail/8105566https://avg.163.com/topic/detail/8104264https://avg.163.com/topic/detail/8106223https://avg.163.com/topic/detail/8121212https://avg.163.com/topic/detail/8121220https://avg.163.com/topic/detail/8121235https://avg.163.com/topic/detail/8103530https://avg.163.com/topic/detail/8121260https://avg.163.com/topic/detail/8104258https://avg.163.com/topic/detail/8121280https://avg.163.com/topic/detail/8104901https://avg.163.com/topic/detail/8121239https://avg.163.com/topic/detail/8105575https://avg.163.com/topic/detail/8121254https://avg.163.com/topic/detail/8121210https://avg.163.com/topic/detail/8121278https://avg.163.com/topic/detail/8121233https://avg.163.com/topic/detail/8121301https://avg.163.com/topic/detail/8121257https://avg.163.com/topic/detail/8121282https://avg.163.com/topic/detail/8121294https://avg.163.com/topic/detail/8103521https://avg.163.com/topic/detail/8104252https://avg.163.com/topic/detail/8104897https://avg.163.com/topic/detail/8105571https://avg.163.com/topic/detail/8106235https://avg.163.com/topic/detail/8121219https://avg.163.com/topic/detail/8121229https://avg.163.com/topic/detail/8121255https://avg.163.com/topic/detail/8121277https://avg.163.com/topic/detail/8121297https://avg.163.com/topic/detail/8103519https://avg.163.com/topic/detail/8103515https://avg.163.com/topic/detail/8104263https://avg.163.com/topic/detail/8103516https://avg.163.com/topic/detail/8121230https://avg.163.com/topic/detail/8104260https://avg.163.com/topic/detail/8121249https://avg.163.com/topic/detail/8104882https://avg.163.com/topic/detail/8121273https://avg.163.com/topic/detail/8105565https://avg.163.com/topic/detail/8121291https://avg.163.com/topic/detail/8106219https://avg.163.com/topic/detail/8104265https://avg.163.com/topic/detail/8121218https://avg.163.com/topic/detail/8104885https://avg.163.com/topic/detail/8121238https://avg.163.com/topic/detail/8105564https://avg.163.com/topic/detail/8121261https://avg.163.com/topic/detail/8121217https://avg.163.com/topic/detail/8121274https://avg.163.com/topic/detail/8121236https://avg.163.com/topic/detail/8121252https://avg.163.com/topic/detail/8121281https://avg.163.com/topic/detail/8121302https://avg.163.com/topic/detail/8103527https://avg.163.com/topic/detail/8104249https://avg.163.com/topic/detail/8121216https://avg.163.com/topic/detail/8121228https://avg.163.com/topic/detail/8103523https://avg.163.com/topic/detail/8121248https://avg.163.com/topic/detail/8104246https://avg.163.com/topic/detail/8121271https://avg.163.com/topic/detail/8104878https://avg.163.com/topic/detail/8121289https://avg.163.com/topic/detail/8105570https://avg.163.com/topic/detail/8103513https://avg.163.com/topic/detail/8104259https://avg.163.com/topic/detail/8104876https://avg.163.com/topic/detail/8103520https://avg.163.com/topic/detail/8104250https://avg.163.com/topic/detail/8105561https://avg.163.com/topic/detail/8106224https://avg.163.com/topic/detail/8104889https://avg.163.com/topic/detail/8105574https://avg.163.com/topic/detail/8106245https://avg.163.com/topic/detail/8121213https://avg.163.com/topic/detail/8106227https://avg.163.com/topic/detail/8121214https://avg.163.com/topic/detail/8121234https://avg.163.com/topic/detail/8121258https://avg.163.com/topic/detail/8121279https://avg.163.com/topic/detail/8121292https://avg.163.com/topic/detail/8121232https://avg.163.com/topic/detail/8121211https://avg.163.com/topic/detail/8121259https://avg.163.com/topic/detail/8121237https://avg.163.com/topic/detail/8121272https://avg.163.com/topic/detail/8121256https://avg.163.com/topic/detail/8121299https://avg.163.com/topic/detail/8121283https://avg.163.com/topic/detail/8121298https://avg.163.com/topic/detail/8103510https://avg.163.com/topic/detail/8104253https://avg.163.com/topic/detail/8104877https://avg.163.com/topic/detail/8105562https://avg.163.com/topic/detail/8106231https://avg.163.com/topic/detail/8121227https://avg.163.com/topic/detail/8121251https://avg.163.com/topic/detail/8121275https://avg.163.com/topic/detail/8103509https://avg.163.com/topic/detail/8104251https://avg.163.com/topic/detail/8104883https://avg.163.com/topic/detail/8105569https://avg.163.com/topic/detail/8106243https://avg.163.com/topic/detail/8121209https://avg.163.com/topic/detail/8121225https://avg.163.com/topic/detail/8121247https://avg.163.com/topic/detail/8121269https://avg.163.com/topic/detail/8121296https://avg.163.com/topic/detail/8103506https://avg.163.com/topic/detail/8104255https://avg.163.com/topic/detail/8104880https://avg.163.com/topic/detail/8105559https://avg.163.com/topic/detail/8106247https://avg.163.com/topic/detail/8121208https://avg.163.com/topic/detail/8121226https://avg.163.com/topic/detail/8121253https://avg.163.com/topic/detail/8121270https://avg.163.com/topic/detail/8121290https://avg.163.com/topic/detail/8121167https://avg.163.com/topic/detail/8121170https://avg.163.com/topic/detail/8121173https://avg.163.com/topic/detail/8121180https://avg.163.com/topic/detail/8121148https://avg.163.com/topic/detail/8121153https://avg.163.com/topic/detail/8121158https://avg.163.com/topic/detail/8121162https://avg.163.com/topic/detail/8121130https://avg.163.com/topic/detail/8121133https://avg.163.com/topic/detail/8121137https://avg.163.com/topic/detail/8121139https://avg.163.com/topic/detail/8121112https://avg.163.com/topic/detail/8121116https://avg.163.com/topic/detail/8121120https://avg.163.com/topic/detail/8121124https://avg.163.com/topic/detail/8117422https://avg.163.com/topic/detail/8117429https://avg.163.com/topic/detail/8117437https://avg.163.com/topic/detail/8117874https://avg.163.com/topic/detail/8116330https://avg.163.com/topic/detail/8116332https://avg.163.com/topic/detail/8116464https://avg.163.com/topic/detail/8113729https://avg.163.com/topic/detail/8113257https://avg.163.com/topic/detail/8113303https://avg.163.com/topic/detail/8113328https://avg.163.com/topic/detail/8110423https://avg.163.com/topic/detail/8110372https://avg.163.com/topic/detail/8110385https://avg.163.com/topic/detail/8110384https://avg.163.com/topic/detail/8110378https://avg.163.com/topic/detail/8110377https://avg.163.com/topic/detail/8110371https://avg.163.com/topic/detail/8110381https://avg.163.com/topic/detail/8110367https://avg.163.com/topic/detail/8110375https://avg.163.com/topic/detail/8110382https://avg.163.com/topic/detail/8110368https://avg.163.com/topic/detail/8110376https://avg.163.com/topic/detail/8110370https://avg.163.com/topic/detail/8110380https://avg.163.com/topic/detail/8110369https://avg.163.com/topic/detail/8110374https://avg.163.com/topic/detail/8110379https://avg.163.com/topic/detail/8110366https://avg.163.com/topic/detail/8110416https://avg.163.com/topic/detail/8110385https://avg.163.com/topic/detail/8110382https://avg.163.com/topic/detail/8110384https://avg.163.com/topic/detail/8110380https://avg.163.com/topic/detail/8110379https://avg.163.com/topic/detail/8110381https://avg.163.com/topic/detail/8110378https://avg.163.com/topic/detail/8110372https://avg.163.com/topic/detail/8110377https://avg.163.com/topic/detail/8110375https://avg.163.com/topic/detail/8110371https://avg.163.com/topic/detail/8110370https://avg.163.com/topic/detail/8110368https://avg.163.com/topic/detail/8110337https://avg.163.com/topic/detail/8110343https://avg.163.com/topic/detail/8110373https://avg.163.com/topic/detail/8110369https://avg.163.com/topic/detail/8110367https://avg.163.com/topic/detail/8110366https://avg.163.com/topic/detail/8110342https://avg.163.com/topic/detail/8110347https://avg.163.com/topic/detail/8110346https://avg.163.com/topic/detail/8110345https://avg.163.com/topic/detail/8110344https://avg.163.com/topic/detail/8110343https://avg.163.com/topic/detail/8110342https://avg.163.com/topic/detail/8110341https://avg.163.com/topic/detail/8110339https://avg.163.com/topic/detail/8110340https://avg.163.com/topic/detail/8110338https://avg.163.com/topic/detail/8110337https://avg.163.com/topic/detail/8110336https://avg.163.com/topic/detail/8110335https://avg.163.com/topic/detail/8110302https://avg.163.com/topic/detail/8110301https://avg.163.com/topic/detail/8110258https://avg.163.com/topic/detail/8110257https://avg.163.com/topic/detail/8110253https://avg.163.com/topic/detail/8110296https://avg.163.com/topic/detail/8110295https://avg.163.com/topic/detail/8110258https://avg.163.com/topic/detail/8110257https://avg.163.com/topic/detail/8110256https://avg.163.com/topic/detail/8110253https://avg.163.com/topic/detail/8110252https://avg.163.com/topic/detail/8098242https://avg.163.com/topic/detail/8098270https://avg.163.com/topic/detail/8098297https://avg.163.com/topic/detail/8098355https://avg.163.com/topic/detail/8098247https://avg.163.com/topic/detail/8098238https://avg.163.com/topic/detail/8098279https://avg.163.com/topic/detail/8098232https://avg.163.com/topic/detail/8098306https://avg.163.com/topic/detail/8098301https://avg.163.com/topic/detail/8098276https://avg.163.com/topic/detail/8098228https://avg.163.com/topic/detail/8098274https://avg.163.com/topic/detail/8098237https://avg.163.com/topic/detail/8098302https://avg.163.com/topic/detail/8098261https://avg.163.com/topic/detail/8098328https://avg.163.com/topic/detail/8098235https://avg.163.com/topic/detail/8098266https://avg.163.com/topic/detail/8098246https://avg.163.com/topic/detail/8098278https://avg.163.com/topic/detail/8098244https://avg.163.com/topic/detail/8098309https://avg.163.com/topic/detail/8098226https://avg.163.com/topic/detail/8098234https://avg.163.com/topic/detail/8098280https://avg.163.com/topic/detail/8098258https://avg.163.com/topic/detail/8098287https://avg.163.com/topic/detail/8098289https://avg.163.com/topic/detail/8098190https://avg.163.com/topic/detail/8098214https://avg.163.com/topic/detail/8098248https://avg.163.com/topic/detail/8098313https://avg.163.com/topic/detail/8098141https://avg.163.com/topic/detail/8097984https://avg.163.com/topic/detail/8097994private:int value; // 常规成员不能在 const 函数中修改mutable int cachedValue; // 可修改成员可以在 const 函数中修改};int main() {const Example ex;// ex.increment(); // 错误无法在 const 对象上调用非 const 函数// ex.value 10; // 错误无法修改 const 对象的成员std::cout Value: ex.getValue() std::endl;std::cout Cached Value: ex.getCachedValue() std::endl; // 输出为 0return 0;}适用场景缓存在const函数中计算并缓存结果而不影响对象的外部状态。状态跟踪如日志计数器跟踪调用次数等信息避免对类的逻辑进行侵入式修改。注意事项mutable变量的使用应谨慎以免导致意外的状态变化影响代码的可读性和可维护性。mutable适用于需要在const环境中更改状态的特定情况而不是普遍的设计模式。thread_local 存储类thread_local 是 C11 引入的一种存储类用于在多线程环境中管理线程特有的变量。使用 thread_local 修饰的变量在每个线程中都有独立的实例因此每个线程对该变量的操作不会影响其他线程。独立性每个线程都有自己独立的变量副本不同线程之间的读写操作互不干扰。生命周期thread_local变量在其线程结束时自动销毁。初始化thread_local变量可以进行静态初始化或动态初始化支持在声明时初始化。thread_local 适合用于需要存储线程状态、缓存或者避免数据竞争的场景如线程池、请求上下文等。以下演示了可以被声明为 thread_local 的变量#include iostream #include thread thread_local int threadSpecificVar 0; // 每个线程都有自己的 threadSpecificVar void threadFunction(int id) { threadSpecificVar id; // 设置线程特有的变量 std::cout Thread id : threadSpecificVar threadSpecificVar std::endl; } int main() { std::thread t1(threadFunction, 1); std::thread t2(threadFunction, 2); t1.join(); t2.join(); return 0; }注意事项性能由于每个线程都有独立的副本thread_local变量的访问速度可能比全局或静态变量稍慢。静态存储thread_local变量的存储类型为静态存储持续时间因此在程序整个运行期间会一直存在。