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]);
Solution
The output of the code will be 30.
Here's the step by step explanation:
-
The code declares an array
arrof integers with 5 elements: 10, 20, 30, 40, 50. -
The
printffunction is used to print the value of an element in the array. -
The unusual part here is
2[arr]. In C and C++,array[index]is actually syntactic sugar for*(array + index). This means that2[arr]is equivalent to*(2 + arr), which is the same as*(arr + 2), orarr[2]. -
So,
2[arr]is accessing the third element in the array (since array indices start at 0), which is the number 30. -
Therefore, the output of the code is 30.
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
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.