Machine-translated by Google. Questions are authored in English.
Top

Discussion

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;
}

  • A.0 1 2 3 4
  • B.Syntax Error
  • C.Semantic Error
  • D.Garbage Value

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.