Knowee
Questions
Features
Study Tools

What is the output of the following code?int arr[] = {10, 20, 30, 40, 50};printf("%d\n", 2[arr]);

Question

What is the output of the following code?int arr[] = {10, 20, 30, 40, 50};printf("%d\n", 2[arr]);

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

Solution

The output of the code will be 30.

Here's the step by step explanation:

  1. The code declares an array arr of integers with 5 elements: 10, 20, 30, 40, 50.

  2. The printf function is used to print the value of an element in the array.

  3. The unusual part here is 2[arr]. In C and C++, array[index] is actually syntactic sugar for *(array + index). This means that 2[arr] is equivalent to *(2 + arr), which is the same as *(arr + 2), or arr[2].

  4. So, 2[arr] is accessing the third element in the array (since array indices start at 0), which is the number 30.

  5. Therefore, the output of the code is 30.

This problem has been solved

Similar Questions

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

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

.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. What will be the output?#include <stdio.h>int main() {    int arr[3] = {10, 20, 30};    int *p = arr;    *(p + 1) = 40;    printf("%d\n", arr[1]);    return 0;}

What will be the output of the following C program?#include<stdio.h>int main() {     int arr[1]={10};     printf("%d", arr[0]);     return 0; }Select one:01106

1/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.