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);
Question
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);
Solution
The correct way to initialize an array in C is option c. Here's how you do it:
-
Declare the array with a specific size. In this case, the size is 3. So, you write
int arr[3]
. -
Initialize the array with specific values. In this case, the values are 1, 2, and 3. So, you write
= {1,2,3};
. -
Put it all together. So, the final code is
int arr[3] = {1,2,3};
.
The other options are incorrect because they either use parentheses instead of square brackets, which is not the correct syntax for declaring an array in C, or they use parentheses instead of curly braces, which is not the correct syntax for initializing an array in C.
Similar Questions
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};
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 declare and initialize a one-dimensional array in C?
How can you define an array in C/C++?*0/1A. array int[] B. int array{}C. int[] arrayD. int array[]
Explain different ways in which you can declare and initialize a single dimensionalarray.
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.