What is the correct way to declare and initialize a one-dimensional array in C?
Question
What is the correct way to declare and initialize a one-dimensional array in C?
Solution
In C, you can declare and initialize a one-dimensional array as follows:
int arrayName[size];
Here, int
is the data type of the array, arrayName
is the name of the array, and size
is the number of elements that the array can hold.
To initialize the array at the time of declaration, you can do:
int arrayName[] = {element1, element2, element3, ...};
Here, element1, element2, element3, ...
are the values with which the array is initialized. The size of the array is determined by the number of values provided in the curly braces {}
.
For example, an array of integers can be declared and initialized as follows:
int numbers[] = {10, 20, 30, 40, 50};
In this case, the array numbers
has 5 elements. The first element numbers[0]
is 10
, the second element numbers[1]
is 20
, and so on.
Similar Questions
Explain different ways in which you can declare and initialize a single dimensionalarray.
How do you initialize an array in C?a.int arr[3] = (1,2,3);b.int arr(3) = {1,2,3};c.int arr[3] = {1,2,3};d.int arr(3) = (1,2,3);
How is an array initialized in C language?int (a)=[1,2,3];int a[3]={1,2,3};int a[]=new int[3]int a={1,2,3};
What is the correct way to initialize a 1D array named grades with values 850, 900, 780, 920, and 880 in C?
Which of the following is the correct way to initialize an array in C++?int arr[3] = {1, 2, 3};int arr = {1, 2, 3};int arr[3] = 1, 2, 3;int arr[] = {1, 2, 3};
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.