Knowee
Questions
Features
Study Tools

What is the output of the following code?int arr[5] = {1, 2, 3};printf("%d\n", arr[4]);

Question

What is the output of the following code?int arr[5] = {1, 2, 3};printf("%d\n", arr[4]);

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be 0.

Explanation: In C, when you declare an array and initialize it with fewer elements than the declared size, the remaining elements will be automatically initialized to 0. So, in this case, the array arr[5] will look like this: {1, 2, 3, 0, 0}. Therefore, when you print arr[4], it will output 0.

Similar Questions

What will be the output of the following code?#include <stdio.h>int main() {    int arr[4] = {0, 1, 2, 3};    printf("%d\n", arr[-1]);    return 0;}

What is the output of the following code?#include <stdio.h>int main() {    int arr[] = {1, 2, 3, 4, 5};    int *p = arr;    printf("%d\n", *(p + 3));    return 0;}

.What will be the output of the following C code?int main() {int arr[] = {1, 2, 3, 4, 5};printf("%d", arr[3]);return 0;}*1 point1234

Consider the following code snippet. What is the output?#include <stdio.h>int main() {    int arr[5] = {1, 2};    for (int i = 0; i < 5; i++) {        printf("%d ", arr[i]);    }    return 0;}

What will be the output of the following C code? #include int main() { int ary[4] = {1, 2, 3, 4}; printf("%d\n", *ary); }a.1b.Compile time errorc.Some garbage valued.Undefined variable

1/2

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.