Q:

how to initialize an array in c

// Array declaration by specifying size 
int arr1[10]; 
  
// With recent C/C++ versions, we can also 
// declare an array of user specified size 
int n = 10; 
int arr2[n]; 
// Array declaration by initializing elements 
int arr[] = { 10, 20, 30, 40 } 

// Compiler creates an array of size 4. 
// above is same as "int arr[4] = {10, 20, 30, 40}"
// Array declaration by specifying size and initializing 
// elements 
int arr[6] = { 10, 20, 30, 40 } 

// Compiler creates an array of size 6, initializes first 
// 4 elements as specified by user and rest two elements as 0. 
// above is same as "int arr[] = {10, 20, 30, 40, 0, 0}" 

14
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
4
type arrayName [ arraySize ];
9
int x[] = {1,2,3}; // x has type int[3] and holds 1,2,3
int y[5] = {1,2,3}; // y has type int[5] and holds 1,2,3,0,0
int z[3] = {0}; // z has type int[3] and holds all zeroes
0

New to Communities?

Join the community