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);
}
- A.
world
- B.
world5
- C.
world10
- 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.
char arr[10];declares a 10-byte character array; the expressionarrdecays to the address ofarr[0]but is not itself a reassignable pointer.arr = "world";attempts to make the namearrpoint somewhere else — the address of the string literal"world".Because
arris 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 reachesprintf()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.