How is an array initialized in C language?

How is an array initialized in C language?

a) int arr[5] = {1, 2, 3, 4, 5};
b) int arr(5) = {1, 2, 3, 4, 5};
c) int arr[] = (1, 2, 3, 4, 5);
d) int arr{} = {1, 2, 3, 4, 5};

Answer:

a) int arr[5] = {1, 2, 3, 4, 5};

Explanation:

In C, an array is initialized by specifying its size and assigning values within curly braces. For example, int arr[5] = {1, 2, 3, 4, 5}; initializes an array of integers with five elements. If fewer values are provided than the size of the array, the remaining elements are automatically initialized to zero.

Proper initialization of arrays is important to ensure that they contain valid data before use in a program.

Reference links:

https://www.rameshfadatare.com/learn-c-programming/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top