1

When I try to make "strcat" function for practice, I find some difference between 'for' loop with braces and without braces. But I have questions about the reason.

At first, my code is like

char strcat(char *ad,char *cp)
{
    int i=0,j;

    for(i;ad[i];i++)  ///////////// here!!

    for(j=0;ad[i]=cp[j];j++){
        i++;
    }

    return ad;
}

In this code, 'strcat' doesn't work because of 'for' loop. With curly-braces, it works. I just learn "'for' loop works with curly-braces" by rote.
But when we translate 'for' to while

i=0
while(str[i])
i++;

We didn't use curly braces, but it works.

I want to know reason about this.

Luke
  • 11
  • 1
  • Hint 1: you're missing a semicolon somewhere. Use an editor able to indent C code, and you'll realize where. Hint 2: There are more issues with your code. Turn on all warnings in your compiler, and read what the compiler tells you. – Markus Oct 25 '19 at 10:12
  • Not sure if I understand your question correctly, but is this a duplicate of https://stackoverflow.com/questions/26289546/what-does-a-for-loop-without-curly-braces-do ? – Len Oct 25 '19 at 10:30

0 Answers0