C syntax

1 week ago 3

Storage class specifiers: C23 changes the auto keyword's meaning

← Previous revision Revision as of 17:00, 4 July 2025
Line 388: Line 388:
:<sup>1</sup> Allocated and deallocated using the {{code|malloc()}} and {{code|free()}} library functions.
:<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}}
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 C23 and 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.
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.
Open Full Post