C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 11)
11.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    static char str1[] = "dills";
    static char str2[20];
    static char str3[] = "Daffo";
    int i;
    i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
    printf("%d\n", i);
    return 0;
}
0
1
2
4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
31 comments Page 2 of 4.

Harish said:   8 years ago
Str1=dills
Strcpy(str2,str1) it gives str2 = dills
Strcat(str3,str2) gives str3 = daffodills
Strcmp(str3,"daffodils")=it gives 0 ,in string comparison if condition is true it will return 0 else 1.

Satwika said:   9 years ago
Nice explanation @Gautam.

Konstantinos said:   1 decade ago
static char str3[] = "Daffo";
//6 chars length (5+1) memory reserved!

strcat (str3, strcpy (str2, str1));
//copy in to a 6 chars reserved memory 11 chars (10+1) !

As you can see from the below.

char *strcat (char *dest, const char *src).

Dest -- This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string.

From what you see the above should produce a memory corruption!

Anusha.v said:   1 decade ago
I agree with @Goutham's answer, but yet I didn't understood what is the role of static char in this programme?

Shabana said:   1 decade ago
Condition is that string cmp.

1- Strcpy. the str1 is copy to str2 it will print dills.
2- The str2 and str3 multiply done and it will DOffodills it will matches the Daffo. Both ASCII values are same so it will 0.

Harsh said:   1 decade ago
As strings are immutable how is the strings modified.

Gautam said:   1 decade ago
It is simple.

strcpy(str2,str1) : here "dills" will be copied to str2.
Hence str2 contains " dills"

strcat(str3,str2) : "dills" will be concatenated with "daffo"
Hence str3 contains " Daffodills ".

At last strcmp compares with the string str3 with "Daffodills" and pass the value "0" since the string matches.

Raju Singh Kumai said:   1 decade ago
i=strcmp(strcat(str3,strcpy(str2,str1)),"daffodills");


The statement will be solved in this order :

1-> strcpy(str2,str1))

This will cause the value of str1 "dills" to be copied in str2.

2->strcat(str3,strcpy(str2,str1)))

The value of strcpy(str2,str1) will be -> "dills"

So the result of iot will be "daffodills"

3->strcmp(strcat(str3,strcpy(str2,str1)),"daffodills");
Or in short[strcmp("daffodills","daffodills")]

In strcmp if both are same then 0 is return otherwise -1 or 1 is return depending on Left or right Exp is Greater.

So 0 will be return.

Mano said:   1 decade ago
i=strcmp(strcat(str3,strcpy(str2,str1)),"daffodills");

//str1=dills
//str2=char[20]
//str3=daffo

1. strrcpy(str2,str1):

str2=dills //str1 value is copied to str2

2. strcat(str3,str2):

daffodills//str3+str2

3. strcmp(daffodills,daffodills)

They are equal hence 0 is correct.

Kritika said:   1 decade ago
strings are immutable and of reference type, that's y this copy in str3 is allowed.


Post your comments here:

Your comments will be displayed after verification.