C

Chandani • 9.82K Points
Tutor III

Q. What will be the output of the following C code?

Code:
#include <stdio.h>

int main()
{
    char str1[] = "Hello";
    char str2[10];

    str2 = str1;

    printf("%s,%s", str1, str2);

    return 0;
}
  • (A) Hello,
  • (B) Hello,Hello
  • (C) Hello,HelloHello
  • (D) Error

Explanation by: Chandani
There will be a compilation error, because we cannot assign a string like this (str2 = str1). To resolve this issue, we have to use strcpy(str2,str1).

The output will be:

main.c:8:10: error: assignment to expression with array type
    8 |     str2 = str1;
      |          ^

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.


Question analytics