- Array is collection of similar data items.
- An array can hold multiple values of same type of data
- In c programming language an array can be declared as datatype arrayName [ arraySize ];
- In c , while declaring an array we need to specify type of array , name of array and size of the array.
- One can directly initialize an array while declaring itself with some values
- int arrayVariable[2]=[10,20];
- int count[]=[1,2,3,4,5]
- directly initialize an array of particular : count[3]=10
- Let us discuss about how to initialize an array with some default values.
- Using for loop or while loop we can iterate an array and by accessing each index we can assign values
- If we are not initializing an array and try to print all the values then it will print some garbage values.
- c initialize array of ints
#1Write a C program to declare an array and print default values
- #include <stdio.h>
- // write a c program to explain how to initialize an array
- int main () {
- int number[ 15 ];
- int i,k;
- for (k = 0; k < 15; k++ ) {
- printf("Array[%d] = %d\n", k, number[k] );
- }
- getch();
- }
Output:
- Array[0] = -1551778920
- Array[1] = -2
- Array[2] = 1971427546
- Array[3] = 1971495162
- Array[4] = 5189464
- Array[5] = 5181048
- Array[6] = 2686776
- Array[7] = 1971494685
- Array[8] = 0
- Array[9] = 0
- Array[10] = 2686832
- Array[11] = 200
- Array[12] = 192
- Array[13] = 7161720
- Array[14] = 48
#2 write a C Program to initialize an array values to 0 (zeros).
Output:
- Array[0] = 0
- Array[1] = 0
- Array[2] = 0
- Array[3] = 0
- Array[4] = 0
- Array[5] = 0
- Array[6] = 0
- Array[7] = 0
- Array[8] = 0
- Array[9] = 0
- Array[10] = 0
- Array[11] = 0
- Array[12] = 0
- Array[13] = 0
- Array[14] = 0
No comments