Examveda
Examveda

What will be the output of the program?
#include<stdio.h>
#include<string.h>
void main()
{
    char str1[20] = "Hello", str2[20] = " World";
    printf("%s", strcpy(str2, strcat(str1, str2)));
}

A. Hello World

B. World

C. WorldHello

D. Hello

E. None of these

Answer: Option A

Solution(By Examveda Team)

>> char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively.

>> printf("%sn", strcpy(str2, strcat(str1, str2)));

=> strcat(str1, str2) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".

=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.

Hence it prints "Hello World".

This Question Belongs to C Program >> Arrays And Strings

Join The Discussion

Comments ( 4 )

  1. Shreya Joshi
    Shreya Joshi :
    6 years ago

    Why it is not world hello world

  2. Srav Shivkumar
    Srav Shivkumar :
    6 years ago

    when concatenated str1 is copied to str 2 wouldn't it be worldhelloworld?
    can some someone explain y its is only hello world

  3. Savi Pokharia
    Savi Pokharia :
    6 years ago

    i didnt get the logic

  4. Karim Sahli
    Karim Sahli :
    7 years ago

    The result will be "hello world"

Related Questions on Arrays and Strings