• 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
  1. #include <stdio.h>
  2. // write a c program to explain how to initialize an array
  3. int main () {

  4.    int number[ 15 ]; 
  5.    int i,k;
  6.    
  7.    
  8.    for (k = 0; k < 15; k++ ) {
  9.       printf("Array[%d] = %d\n", k, number[k] );
  10.    }
  11.  
  12.   getch();
  13. }


Output:
  1. Array[0] = -1551778920
  2. Array[1] = -2
  3. Array[2] = 1971427546
  4. Array[3] = 1971495162
  5. Array[4] = 5189464
  6. Array[5] = 5181048
  7. Array[6] = 2686776
  8. Array[7] = 1971494685
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 2686832
  12. Array[11] = 200
  13. Array[12] = 192
  14. Array[13] = 7161720
  15. Array[14] = 48


#2 write a C Program to initialize an array values to 0 (zeros).

initialize an array in c


Output:
  1. Array[0] = 0
  2. Array[1] = 0
  3. Array[2] = 0
  4. Array[3] = 0
  5. Array[4] = 0
  6. Array[5] = 0
  7. Array[6] = 0
  8. Array[7] = 0
  9. Array[8] = 0
  10. Array[9] = 0
  11. Array[10] = 0
  12. Array[11] = 0
  13. Array[12] = 0
  14. Array[13] = 0
  15. Array[14] = 0

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu