Which of the following is the correct way to initialize
2022
Which of the following is the correct way to initialize an array?
- A.
int num[6] = (2, 4, 12, 5, 45, 5)
- B.
int num[ ] = {2, 4, 12, 5, 45, 5}
- C.
int num{6} = {2, 4, 12}
- D.
int num(6) = {2, 4, 12, 5, 45, 5}
Attempted by 549 students.
Show answer & explanation
Correct answer: B
Correct C syntax for array initialization uses curly braces { } , not parentheses. Correct initialization example: int num[] = {2, 4, 12, 5, 45, 5}; You can omit the size in the square brackets; the compiler infers the array length from the number of initializer elements.