Static keywords are used in C to change the life cycle or access scope of a variable or function, depending on the context. ① The static variables in the function have a global life cycle, but are only visible within the function, and initialization is only performed once; ② The static members in the class belong to the entire class rather than the object, and all instances share the same data; ③ The static function can only access static members and can be called without creating an object; ④ The static in the global scope restricts the scope of the variable or function to the current file, encapsulation; ⑤ When using it, it is necessary to note that the static members in the class must be defined outside the class, thread safety of local static variables, and avoid abuse. Mastering static can improve code efficiency and structural clarity, but it also needs to be used with caution.
Static keyword in C is a keyword that is easy to use but not easy to understand thoroughly. It has different functions in different contexts, including static variables, static functions, static members, etc. Mastering its use can help you write more efficient and clearer code.

What are static variables?
Static variables are most commonly used inside functions and in classes.

Static variables in the function:
When you declare a static variable in a function, the life cycle of this variable will run throughout the program, but it will only be initialized once the first time it is executed to the declaration.

void count() { static int counter = 0; std::cout << counter << std::endl; }
Each time the count()
function is called, counter
value is incremented, rather than resetting to 0 each time. This is because the static variable is not destroyed as the function call ends.
Static member variables in the class:
The static member in the class belongs to the entire class, not an object of the class. That is, no matter how many objects are created, there is only one copy of the static member.
For example:
class MyClass { public: static int count; MyClass() { count ; } }; int MyClass::count = 0; // Must be defined outside the class
In this way, count
will be added when each object is constructed to count how many objects have been created.
What's special about the static function?
The static function in the class can only access static member variables and other static functions. Because it does not have this pointer, it cannot be bound to a specific object.
For example:
class Utility { public: static void printVersion() { std::cout << "Version: " << version << std::endl; } private: static std::string version; }; std::string Utility::version = "1.0";
In this example, printVersion
is a static function that can only access static member version
. If you try to access non-static members, the compiler will report an error.
The advantage of static functions is that they can be called directly without creating objects, such as tool classes or helper functions that are very commonly used in this way.
What does static do in global scope?
Using the static keyword in a global scope (i.e. not in a class or function) affects the linkability of a variable or function.
- static global variable: Its scope is limited to the current file and other files cannot be accessed through extern.
- static function: It can also be used only in the files that define it to prevent naming conflicts.
for example:
// file1.cpp static int secret = 42; void showSecret() { std::cout << secret << std::endl; }
Other source files cannot access secret
, even if they know the name, it is useless. This is useful for package implementation details.
Several things to note when using static
- The static members in the class must be defined separately outside the class, otherwise an error occurs when linking.
- Static local variables can cause problems in multithreaded environments unless the initialization is thread-safe (C 11 The default thread-safe initialization of local static variables).
- Don't abuse static, especially in class designs, overuse can make the code difficult to maintain and test.
Basically that's it.
The key to understanding static is to remember that it changes the life cycle or access scope of a variable or function, and the specific behavior depends on the context used.
The above is the detailed content of C tutorial explaining the static keyword. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The answer is: Use the std::string constructor to convert the char array to std::string. If the array contains the intermediate '\0', the length must be specified. 1. For C-style strings ending with '\0', use std::stringstr(charArray); to complete the conversion; 2. If the char array contains the middle '\0' but needs to convert the first N characters, use std::stringstr(charArray,length); to clearly specify the length; 3. When processing a fixed-size array, make sure it ends with '\0' and then convert it; 4. Use str.assign(charArray,charArray strl

If it is iterating when deleting an element, you must avoid using a failed iterator. ①The correct way is to use it=vec.erase(it), and use the valid iterator returned by erase to continue traversing; ② The recommended "erase-remove" idiom for batch deletion: vec.erase(std::remove_if(vec.begin(),vec.end(), condition), vec.end()), which is safe and efficient; ③ You can use a reverse iterator to delete from back to front, the logic is clear, but you need to pay attention to the condition direction. Conclusion: Always update the iterator with the erase return value, prohibiting operations on the failed iterator, otherwise undefined behavior will result.

TheautokeywordinC deducesthetypeofavariablefromitsinitializer,makingcodecleanerandmoremaintainable.1.Itreducesverbosity,especiallywithcomplextypeslikeiterators.2.Itenhancesmaintainabilitybyautomaticallyadaptingtotypechanges.3.Itisnecessaryforunnamed

Use std::source_location::current() as the default parameter to automatically capture the file name, line number and function name of the call point; 2. You can simplify log calls through macros such as #defineLOG(msg)log(msg,std::source_location::current()); 3. You can expand the log content with log level, timestamp and other information; 4. To optimize performance, function names can be omitted or location information can be disabled in the release version; 5. Column() and other details are rarely used, but are available. Using std::source_location can significantly improve the debugging value of logs with extremely low overhead without manually passing in FIL

std::mutex is used to protect shared resources to prevent data competition. In the example, the automatic locking and unlocking of std::lock_guard is used to ensure multi-thread safety; 1. Using std::mutex and std::lock_guard can avoid the abnormal risks brought by manual management of locks; 2. Shared variables such as counters must be protected with mutex when modifying multi-threads; 3. RAII-style lock management is recommended to ensure exception safety; 4. Avoid deadlocks and multiple locks in a fixed order; 5. Any scenario of multi-thread access to shared resources should use mutex synchronization, and the final program correctly outputs Expected:10000 and Actual:10000.

memory_order_relaxed is suitable for scenarios where only atomicity is required without synchronization or order guarantee, such as counters, statistics, etc. 1. When using memory_order_relaxed, operations can be rearranged by the compiler or CPU as long as the single-threaded data dependency is not destroyed. 2. In the example, multiple threads increment the atomic counter, because they only care about the final value and the operation is consistent, the relaxed memory order is safe and efficient. 3. Fetch_add and load do not provide synchronization or sequential constraints when using relaxed. 4. In the error example, the producer-consumer synchronization is implemented using relaxed, which may cause the consumer to read unupdated data values because there is no order guarantee. 5. The correct way is

The most common method of finding vector elements in C is to use std::find. 1. Use std::find to search with the iterator range and target value. By comparing whether the returned iterator is equal to end(), we can judge whether it is found; 2. For custom types or complex conditions, std::find_if should be used and predicate functions or lambda expressions should be passed; 3. When searching for standard types such as strings, you can directly pass the target string; 4. The complexity of each search is O(n), which is suitable for small-scale data. For frequent searches, you should consider using std::set or std::unordered_set. This method is simple, effective and widely applicable to various search scenarios.

Install the Boost library, 2. Write code for DNS resolution using Boost.Asio, 3. Compile and link the boost_system library, 4. Run the program to output the IP address parsed by www.google.com; this example shows how Boost.Asio simplifies network programming in C, implements cross-platform, type-safe synchronous DNS queries through io_context and tcp::resolver, and supports IPv4 and IPv6 address resolution, and finally prints all resolution results.
