-4

I ran this code:

#include<stdio.h>
#include<string.h>

int main()
{
static char str1[] = "dills";
static char str2[20];
static char str3[] = "Daffo";
int i,j;
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
printf("%d", i);printf("\n");
printf("%s",str1);printf("\n");
printf("%s",str2);printf("\n");
printf("%s",str3);printf("\n");
printf("%s",strcpy(str2, str1));printf("\n");
printf("%s",strcat(str3, strcpy(str2, str1)));
return 0;
}

Output:

0
ills
dills
Daffodills
ills
Daffodillsills
  1. How str1 became "ills"?
  2. why strcpy is returning "ills"?
  3. Even if strcat(str3, strcpy(str2, str1)) returns Daffodillsills but the answer of strcmp is 0. Why?
Arnab
  • 19
  • 2
  • 1
    What do you think? Did you analyse the intermediate results? Why not? What does the debugger say? What did you expect? – too honest for this site Aug 19 '16 at 02:21
  • 3
    `str3` is not large enough for your first `strcat()` call. You probably overwrite part of one of your other arrays. – Dmitri Aug 19 '16 at 02:21
  • 1
    You should include the newlines in the first `printf()` format string on each line, rather than calling `printf()` twice per line. That is: `printf("%d", i);printf("\n");` should be `printf("%d\n", i);` – Jonathan Leffler Aug 19 '16 at 02:42

1 Answers1

4

You do have enough space in str2 to contain the concatenated values. But, the ordering of your calls tries to concat into str3 instead of what you probably wanted (i.e. str2). Also, even without the length issue, you'd get dillsDaffo, so further reordering is necessary.

Instead of:

i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");

Try this:

i = strcmp(strcat(strcpy(str2, str3), str1), "Daffodills");

UPDATE:

All the bugs in the original codes are essentially caused by trying to do everything on one single line. It would be much better to split that messy line up in several.

Yes. I was debating doing an edit about this. So, here goes ...

Normally, when I do such things I usually do each concatenation separately. I also prep the target buffer so that all concatentations can be strcat (vs. the first one needing to be strcpy):

str2[0] = 0;
strcat(str2,str3);
strcat(str2,str1);
i = strcmp(str2,"Daffodills");

To me, this is clearer, simpler, and more flexible. More flexible because it is a simple matter to add more concatenations if desired or to reorder them if they are out of sequence.

As with many similar sequences in C, the optimizer will generate code that is just as efficient as the long/compound statement.

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • All the bugs in the original codes are essentially caused by trying to do everything on one single line. It would be much better to split that messy line up in several. – Lundin Aug 19 '16 at 06:33
  • @Lundin I was so close to adding the split example in my original post. Afterwards, I was feeling guilty that I left it out. Then, the question got put on hold. Your comment reinspired me to do what I wanted to do anyway. Thanks for the nudge ... – Craig Estey Aug 19 '16 at 08:09