• Home
  • Array Declaration in C

Array Declaration in C

In C language, an array is declared by specifying the type of the elements, followed by the name of the array, and the size of the array enclosed in square brackets.

Here is an example of declaring an array of integers in C:

int array[10]; // declare an array of 10 integers

In this example, the array is declared with a size of 10, and the type of the elements is int. The elements of the array are uninitialized, and have undefined values.

You can also declare and initialize an array with values:

int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declare and initialize an array of 10 integers

In this example, the array is declared and initialized with the given values, in the order they appear. The size of the array is inferred from the number of values provided.

You can declare an array with a size larger than the number of values provided, and initialize only a subset of the elements:

int array[10] = {0, 1, 2}; // declare and initialize an array of 10 integers, with 3 values

In this example, the first three elements of the array are initialized with the given values, and the rest of the elements are uninitialized, and have undefined values.

You can also declare an array using the typedef keyword:

typedef int Array[10]; // define a new type "Array" as an array of 10 integers

Array array; // declare an array of 10 integers

In this example, the type “Array” is defined as an array of 10 integers. The array is declared using the new type “Array”. This can be useful if you need to use the same type of array multiple times in your code.

You can also declare an array of pointers:

int *array[10]; // declare an array of 10 pointers to integers

In this example, the array is declared as an array of 10 pointers to integers. The elements of the array are uninitialized, and have undefined values.

You can also declare an array of strings:

char *array[10]; // declare an array of 10 pointers to strings (char arrays)

In this example, the array is declared as an array of 10 pointers to char arrays. The elements of the array are uninitialized, and have undefined values.

You can also declare an array of structures:

struct point {
int x;
int y;
};

struct point array[10]; // declare an array of 10 structures of type “struct point”

In this example, the array is declared as an array of 10 structures of type “struct point”. The elements of the array are uninitialized, and have undefined values.