Array Definition in C
The following is the syntax for array definition in C
Syntax:
type arrayName[size];
The type can be any C data type. The arrayName is the name of the array. The size must be an integer constant greater than zero.
int numbers[10];
Array Initialization in C
Arrays can be initialized in two ways. Firstly, the arrays are initialized while declaring it.
Example:
int number[5] = {10, 20, 30, 40, 50};
int newArray[5];
//Initializing the array newArray
for (int i=0; i<sizeof(newArray); i++)
{
newArray[i] = i+1;
}
newArray | 1 | 2 | 3 | 4 | 5 |
0 | 1 | 2 | 3 | 4 |
#include<stdio.h> int main() { int newArray[5]; //Initializing the array newArray for (int i=0; i<sizeof(newArray); i++) { newArray[i] = i+1; } printf("The third element is %d\n", newArray[2]); }
The third element is 3