Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Storage class specifiers==== Every object has a storage class.{{cn|date=April 2025}} This specifies most basically the storage ''duration,'' which may be static (default for global), automatic (default for local), or dynamic (allocated), together with other features (linkage and register hint).{{cn|date=April 2025}} {| class="wikitable" |+ Storage classes |- ! Specifiers ! Lifetime ! Scope ! Default initializer |- | {{code|auto}} | Block (stack) | Block | Uninitialized |- | {{code|register}} | Block (stack or CPU register) | Block | Uninitialized |- | {{code|static}} | Program | Block or compilation unit | Zero |- | {{code|extern}} | Program | Global (entire program) | Zero |- | {{code|_Thread_local}} | Thread | | |- | ''(none)''<sup>1</sup> | Dynamic (heap) | | Uninitialized (initialized to {{code|0}} if using {{code|calloc()}}) |} :<sup>1</sup> Allocated and deallocated using the {{code|malloc()}} and {{code|free()}} library functions. Variables declared within a [[block (programming)|block]] by default have automatic storage, as do those explicitly declared with the [[Automatic variable|{{code|auto}}]]<ref group="note">The meaning of auto is a type specifier rather than a storage class specifier in C++0x</ref> or [[Register (keyword)|{{code|register}}]] storage class specifiers. The {{code|auto}} and {{code|register}} specifiers may only be used within functions and function argument declarations;{{cn|date=April 2025}} as such, the {{code|auto}} specifier is always redundant. Objects declared outside of all blocks and those explicitly declared with the [[Static variable|{{code|static}}]] storage class specifier have static storage duration. Static variables are initialized to zero by default by the [[compiler]].{{cn|date=April 2025}} Objects with automatic storage are local to the block in which they were declared and are discarded when the block is exited. Additionally, objects declared with the {{code|register}} storage class may be given higher priority by the compiler for access to [[Register (computing)|registers]]; although the compiler may choose not to actually store any of them in a register. Objects with this storage class may not be used with the address-of ({{code|&}}) unary operator. Objects with static storage persist for the program's entire duration. In this way, the same object can be accessed by a function across multiple calls. Objects with allocated storage duration are created and destroyed explicitly with [[malloc|{{code|malloc}}]], {{code|free}}, and related functions. The [[External variable|{{code|extern}}]] storage class specifier indicates that the storage for an object has been defined elsewhere. When used inside a block, it indicates that the storage has been defined by a declaration outside of that block. When used outside of all blocks, it indicates that the storage has been defined outside of the compilation unit. The {{code|extern}} storage class specifier is redundant when used on a function declaration. It indicates that the declared function has been defined outside of the compilation unit. The [[Thread-local storage|{{code|_Thread_local}}]] (<code>thread_local</code> in [[C++]], and in C since [[C23 (C standard revision)|C23]],{{cn|date=April 2025}} and in earlier versions of C if the header <code><threads.h></code> is included) storage class specifier, introduced in [[C11 (C standard revision)|C11]], is used to declare a thread-local variable. It can be combined with {{code|static}} or {{code|extern}} to determine linkage.{{explain|reason=probably should link to, or otherwise explain, threads before discussing thread local storage|date=April 2025}} Note that storage specifiers apply only to functions and objects; other things such as type and enum declarations are private to the compilation unit in which they appear.{{cn|date=April 2025}} Types, on the other hand, have qualifiers (see below).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)