Abhishek
Abhishek

Reputation: 11

Different outputs

Why does the first code give a different output from the second code, even though they intend to do the same thing?

while(s[i++]==t[j++]);

while(s[i]==t[j])
        {
            i++;
            j++;
        }

Upvotes: 1

Views: 68

Answers (1)

Alok Singhal
Alok Singhal

Reputation: 96211

The first code increments i and j even when s[i] != t[j], while the second doesn't.

For example, with:

char s[] = "hello";
char t[] = "world";
int i = 0, j = 0;

The first code will have both i and j equal to 1 after the loop, but the second code will have i and j equal to 0.

Upvotes: 6

Related Questions