πŸ“Š C Programming
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
πŸ’¬ Discuss
βœ… Correct Answer: (D) Error

Explanation: 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;
| ^

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

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
217
Total Visits
πŸ“½οΈ
3 y ago
Published
πŸŽ–οΈ
Chandani
Publisher
πŸ“ˆ
87%
Success Rate