What will be the output of the following ‘C’ language code? void main() { char…

2017

What will be the output of the following ‘C’ language code?

void main()

{

char arr[10];

arr = "world";

printf("%s", arr);

}

  1. A.

    world

  2. B.

    world5

  3. C.

    world10

  4. D.

    L-value required error

Attempted by 622 students.

Show answer & explanation

Correct answer: D

Concept: in C, an array name is not a variable that holds a pointer — it is a non-modifiable l-value whose value decays to the address of its first element whenever it is used in an expression. Because there is no actual pointer variable to overwrite, the name itself can never legally appear on the left side of an assignment (=) after declaration — only individual elements (arr[i]) may be assigned.

  1. char arr[10]; declares a 10-byte character array; the expression arr decays to the address of arr[0] but is not itself a reassignable pointer.

  2. arr = "world"; attempts to make the name arr point somewhere else — the address of the string literal "world".

  3. Because arr is not a modifiable l-value, this violates the assignment constraint in the C standard — the compiler flags it at compile time with an “lvalue required” error, so the program never reaches printf() and no runtime output is produced at all.

Cross-check: this is confirmed by the two valid ways to put a string into arr — initializing it at declaration (char arr[10] = "world";) or copying into it at run time with strcpy(arr, "world");. Both write into the array's existing memory rather than reassigning arr itself, which is exactly why direct assignment fails while these alternatives succeed.

The program therefore fails to compile, producing an “L-value required” error — not any printed string — which matches the option stating that exact compilation error.

Explore the full course: Niacl Ao It Specialist