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 1 of 4.

Suraj gupta said:   8 months 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.
(2)

Uday said:   6 years ago
strcmp() will always return the value 0.
(1)

Mohan said:   7 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.
(1)

Sus said:   1 decade ago
First it will copy str1 value to str2, then it will concatenate, and then compare.

Mallesh said:   7 months ago
Here str1 [] = "dills".
str2[20]
str3[] = "Daffo"
int i

The condition is checked by the form precedence and associativity
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills")
printf("%d\n", i)

i = strcmp(strcat(str3, "dills"), "Daffodills")
i = strcmp("Daffodills", "Daffodills")
i = 0
Then, by the stcmp conditions,
if Str 1 == str2 -> 0.
str1 > str2 -> +ve value.
str1 < str2 -> -ve value.

Priya said:   5 years ago
Thanks @Goutham.

Musthak rahaman said:   6 years ago
The actual output for this is the process finished with exit code -6.

Rushikesh Bhosale said:   6 years ago
Thanks @Gautam.

Deepak Johnson said:   6 years ago
@Suryash.

If we compare the strings the value should return to 0...(Not 1).
Because, if s1 = s2 value would return 0.
If s1<s2 value would be less than 0.
If s1>s2 value would be greater than 0.

Suyash said:   6 years ago
If the string is the same it should return 1 not 0?

Please explain to me.


Post your comments here:

Your comments will be displayed after verification.