Home > other >  C language array, learned to remember, is not popular
C language array, learned to remember, is not popular

Time:12-06


This procedure is used to test: array initialization,

(1) define the array must be initialized, don't think that is not initialized, the system will automatically initialized to zero; If you don't initialize, local variables on the stack, each array element value will be a random number;

(2) array initialization: programmers must be at least to the first array element of array elements initialized to 0, the rest of the elements system automatically initialized to zero; In fact, the array is initialized to zero is done at compile time,

(3) the number of initial value is less than the number of array elements. When the number of initial value is less than the number of array elements, the serial initialization corresponding value, at the back of the initialized to 0;

(4) if you explicitly specify the array size, when the number of elements in the specified in the initialization time more than this size can produce error. For example: int array_test [3]={1, 2, 3, 4} beyond the limits of the number of array elements;

(5) when the array definition does not specify the size, when the initialization using initialization list, so the size of the array is determined by the initialization list when the element number,

(6) in general global variables, static variables in the data area, initialized to zero by default (if the specified initial value for the specified value) local array according to the characteristics of the compiler is different,

(7) static local and global variables, data are stored in the global area, so before the main program, the compiler has good memory for its distribution, in C, the initialization happens before the code execution, good memory after compilation phase distribution,
Will be initialized.

*/


# include & lt; Stdio. H>
# include & lt; String. H>
# include & lt; Stdint. H>

Static int array_static_global [6].//define global static array

Int array_global [6].//define global array

Int main ()
{
Int array_1 [6].//only defines an array, not initialized; If you don't initialize, so each array element value will be a random number; A local variable in a stack, its value is random, namely when the value in memory;

Int array_2 [6]={0};//the programmer must be at least to the first array element of array elements initialized to 0, to make the rest of the elements automatically initialized to zero; Array_2 [6]={0}; <=> Array_2 [6]=,0,0,0,0,0 {0};

Int array_3 [6]={1};//this program is not all elements are assigned a value of 1, only the first element is 1, the rest are 0; Array_3 [6]={1}; <=> ,0,0,0,0,0 array_3 [6]={1};

Int array_4 [6]={1, 2, 3};//when the number of initial value is less than the number of array elements, the serial initialization corresponding value, at the back of the initialized to 0; Array_4 [6]={1, 2, 3}; <=> ,2,3,0,0,0 array_4 [6]={1};

Int array_5 []={1, 2, 3, 4, 5};//when the array definition does not specify the size, when the initialization using initialization list, so the size of the array is determined by the initialization list when the element number, array_5 []={1, 2, 3, 4, 5}; <=> Array_5 [5]={1, 2, 3, 4, 5};

Static int array_local_static [6].//static local variables, uninitialized, the system will automatically initialized to zero;


for(int i=0; i<6; I++)

{
Printf (" array_1 [% d]=% d \ n ", I, array_1 [I]);//array_1 [6] uninitialized, the output for the random value

Printf (" array_2 [% d]=% d \ n ", I, array_2 [I]);//array_2 [6] to initialize only the first number, the rest of the system of automatic assignment of 0

Printf (" array_3 [% d]=% d \ n ", I, array_3 [I]);//array_2 [6] is only the first element is 1, the rest of the system of automatic assignment of 0

Printf (" array_4 [% d]=% d \ n ", I, array_4 [I]);//array_2 [6] part of initialization, serial initialization in front of the corresponding value of 1, 2, 3, the back of the system automatically initialized to 0

Printf (" the array_5 [] element of size is % d \ n ", sizeof (array_5)/sizeof (array_5 [0]));//array_5 [] there are five initialization value, so array_5 [] there are five elements: array_5 [5]

Printf (" array_static_global [% d]=% d \ n ", I, array_static_global [I]);//array_static_global [6] for global static array, when an uninitialized, the system automatically initialized to zero;

Printf (" array_global [% d]=% d \ n ", I, array_global [I]);//array_global [6] for global static array, when an uninitialized, the system automatically initialized to zero;

Printf (" array_local_static [% d]=% d \ n ", I, array_local_static [I]);//array_local_static [6] static local variables, after defining an uninitialized, the system will automatically initialized to zero;
}

return 0;

}

CodePudding user response:

Definition, often complain, second, a change is,
  • Related