| Start |
|---|
| Initialization of Variables in C In the absence of explicit initialization, external and Scalar variables may be initialized when they are defined by following the name with an equals sign and an expression: For external and For automatic and For example, see the code snippet below instead of In effect, initialization of automatic variables are just shorthand for assignment statements. Which form to prefer is largely a matter of taste. We generally use explicit assignments, because initializers in declarations are harder to see and further away from the point of use. On the other hand, variables should only be declared when they're about to be used whenever possible. Initializing an array: An array may be initialized by following its declaration with a list of initializers enclosed in braces and separated by commas. For example, to initialize an array days with the number of days in each month: (Note that January is encoded as month zero in this structure.) When the size of the array is omitted, the compiler will compute the length by counting the initializers, of which there are 12 in this case. If there are fewer initializers for an array than the specified size, the others will be zero for all types of variables. It is an error to have too many initializers. There is no standard way to specify repetition of an initializer — but GCC has an extension (opens new window) to do so. In C89/C90 or earlier versions of C, there was no way to initialize an element in the middle of an array without supplying all the preceding values as well. With C99 and above, designated initializers (opens new window) allow you to initialize arbitrary elements of an array, leaving any uninitialized values as zeros. Initializing Character arrays: Character arrays are a special case of initialization; a string may be used instead of the braces and commas notation: is a shorthand for the longer but equivalent: In this case, the array size is six (five characters plus the terminating 1 What happens to a declared, uninitialized variable in C? Does it have a value? (opens new window) 2 Note that a constant expression is defined as something that can be evaluated at compile-time. So, |
| Using designated initializers C99 introduced the concept of designated initializers. These allow you to specify which elements of an array, structure or union are to be initialized by the values following. Designated initializers for array elementsFor a simple type like plain The term in square brackets, which can be any constant integer expression, specifies which element of the array is to be initialized by the value of the term after the In this example, the size of the array is not defined explicitly, so the maximum index specified in the designated initializers dictates the size of the array — which would be 21 elements in the example. If the size was defined, initializing an entry beyond the end of the array would be an error, as usual. Designated initializers for structuresYou can specify which elements of a structure are initialized by using the If elements are not listed, they are default initialized (zeroed). Designated initializer for unionsYou can specify which element of a union is initialize with a designated initializer. Prior to the C standard, there was no way to initialize a Note that C11 allows you to use anonymous union members inside a structure, so that you don't need the Designated initializers for arrays of structures, etcThese constructs can be combined for arrays of structures containing elements that are arrays, etc. Using full sets of braces ensures that the notation is unambiguous. Specifying ranges in array initializersGCC provides an extension (opens new window) that allows you to specify a range of elements in an array that should be given the same initializer: The triple dots need to be separate from the numbers lest one of the dots be interpreted as part of a floating point number (maximimal munch (opens new window) rule). |
| Initializing structures and arrays of structures Structures and arrays of structures can be initialized by a series of values enclosed in braces, one value per member of the structure. Note that the array initialization could be written without the interior braces, and in times past (before 1990, say) often would have been written without them: Although this works, it is not good modern style — you should not attempt to use this notation in new code and should fix the compiler warnings it usually yields. |
Read Article
https://develop.horje.com/learn/1434/reference