On my way to hone my C skills with C Pointers literature, I came across this code. In this problem I have to justify the conclusion. I am familiar with strcat() and strcmp() . I know that strcmp() returns 0 when two lines are passed, the same.
# include <stdio.h> # include <string.h> int main() { static char str1[]="Good"; static char str2[20]; static char str3[20] ="Day"; int l; l = strcmp(strcat(str3, strcpy(str2, str1)), strcat(str3, "good")); printf("%d\n", l); return 0; }
The response indicates 0, which means that the two calculated lines must be the same. I tried to solve this problem in several steps.
First tried strcat(str3, strcpy(str2, str1)) . 'str2' changes to 'Ok,' then strcat() changes str3 to 'DayGood.' My gcc compiler agrees with me so far.
Going to strcat(str3, "good") , since str3 has already been changed to DayGood , strcat changes str3 to DayGoodgood .
Again, gcc is with me.
int main() { static char str1[]="Good"; static char str2[20]; static char str3[20] ="Day"; int l; printf("%s\n", strcat(str3, strcpy(str2, str1))); printf("%s\n", strcat(str3, "good"));
He produces
DayGood DayGoodgood
I tried this variation again.
int main() { static char str1[]="Good"; static char str2[20]; static char str3[20] ="Day"; int l; printf("%s\n", strcat(str3, "good")); printf("%s\n", strcat(str3, strcpy(str2, str1)));
He produces.
Daygood DaygoodGood
In my both test cases, I get two different lines for comparison. Why then strcmp() creates 0?