Predict the output of the following source code in C language :
int main( )
{
int i, x[5];
for(i=0; i\(\lt\)5; i++) * (x+i) = i;
for(i=0; i\(\lt\)5; i++) printf("%d", x[i]);
return 0;
}
MH SET - 2024, SET Exam - 2024,
Answer: A
The program prints the values 0 1 2 3 4.
In C, *(x+i) is the same element as x[i], since an array name decays to a pointer to its first element. The first loop stores 0, 1, 2, 3, 4 in the five elements, and the second loop prints them in order.
Because the format string is "%d" with no separator, the actual output is the digits run together, 01234; option A lists those values. The code is valid, so there is no syntax or semantic error, and every element is assigned before it is printed, so no garbage value appears.
Please log in or register to report this question.