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!
===Initialization=== Default initialization depends on the [[#Storage class specifiers|storage class specifier]], described above. Because of the language's grammar, a scalar initializer may be enclosed in any number of curly brace pairs. Most compilers issue a warning if there is more than one such pair, though. <syntaxhighlight lang=C>int x = 12; int y = { 23 }; //Legal, no warning int z = { { 34 } }; //Legal, expect a warning</syntaxhighlight> Structures, unions and arrays can be initialized in their declarations using an initializer list. Unless designators are used, the components of an initializer correspond with the elements in the order they are defined and stored, thus all preceding values must be provided before any particular element's value. Any unspecified elements are set to zero (except for unions). Mentioning too many initialization values yields an error. The following statement will initialize a new instance of the structure ''s'' known as ''pi'': <syntaxhighlight lang=C>struct s { int x; float y; char *z; }; struct s pi = { 3, 3.1415, "Pi" };</syntaxhighlight> ====Designated initializers==== Designated initializers allow members to be initialized by name, in any order, and without explicitly providing the preceding values. The following initialization is equivalent to the previous one: <syntaxhighlight lang=C>struct s pi = { .z = "Pi", .x = 3, .y = 3.1415 };</syntaxhighlight> Using a designator in an initializer moves the initialization "cursor". In the example below, if <code>MAX</code> is greater than 10, there will be some zero-valued elements in the middle of <code>a</code>; if it is less than 10, some of the values provided by the first five initializers will be overridden by the second five (if <code>MAX</code> is less than 5, there will be a compilation error): <syntaxhighlight lang=C>int a[MAX] = { 1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0 };</syntaxhighlight> In [[C89 (C version)|C89]], a union was initialized with a single value applied to its first member. That is, the union ''u'' defined above could only have its ''int x'' member initialized: <syntaxhighlight lang=C>union u value = { 3 };</syntaxhighlight> Using a designated initializer, the member to be initialized does not have to be the first member: <syntaxhighlight lang=C>union u value = { .y = 3.1415 }; </syntaxhighlight> If an array has unknown size (i.e. the array was an [[#Incomplete_types|incomplete type]]), the number of initializers determines the size of the array and its type becomes complete: <syntaxhighlight lang=C> int x[] = { 0, 1, 2 } ;</syntaxhighlight> Compound designators can be used to provide explicit initialization when unadorned initializer lists might be misunderstood. In the example below, <code>w</code> is declared as an array of structures, each structure consisting of a member <code>a</code> (an array of 3 <code>int</code>) and a member <code>b</code> (an <code>int</code>). The initializer sets the size of <code>w</code> to 2 and sets the values of the first element of each <code>a</code>: <syntaxhighlight lang=C>struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2 };</syntaxhighlight><!-- Note: The [http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf C99 specification]'s grammar for designations reads in part: designation: designator-list = designator-list: designator designator-list designator designator: [ constant-expression ] . identifier ...which can be understood as making these code fragments legal: int x[] = { [1] [9] = 2 } ; struct t { char *name; char *nickname; } who = { .name .nickname = "Unknown" }; But that is incorrect. The designators that make up the designator-list are *not* space-separated: the grammar means to describe the kind of structure-path given in the example. --> This is equivalent to:<syntaxhighlight lang=C>struct { int a[3], b; } w[] = { { { 1, 0, 0 }, 0 }, { { 2, 0, 0 }, 0 } };</syntaxhighlight> There is no way to specify repetition of an initializer in standard C. ====Compound literals==== It is possible to borrow the initialization methodology to generate compound structure and array literals: <syntaxhighlight lang=C> // pointer created from array literal. int *ptr = (int[]){ 10, 20, 30, 40 }; // pointer to array. float (*foo)[3] = &(float[]){ 0.5f, 1.f, -0.5f }; struct s pi = (struct s){ 3, 3.1415, "Pi" }; </syntaxhighlight> Compound literals are often combined with designated initializers to make the declaration more readable:<ref name="bk21st" /> <syntaxhighlight lang=C>pi = (struct s){ .z = "Pi", .x = 3, .y = 3.1415 };</syntaxhighlight>
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)