1
void strcpy(char *s, char *t)
{
    while ((*s++ = *t++) != '\0');
}

and

void strcpy(char *s, char *t)
{
    while (*s++ = *t++);
}

are the same, what does this mean? what does removing the condition do?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Mod Mark
  • 199
  • 1
  • 2
  • 7

3 Answers3

4

The expression *s++ = *t++ still has a result, and that result that can be used as a condition. More precisely, the result will be the character copied, and as you (should) know all non-zero values are considered "true", and as you also (should) know strings in C are zero terminated.

So what the loop does is copy characters until the string terminator is reached.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
-1

The condition is to check if the end of the string has been reached and not go past it. As you know, in C, strings are ended with a '\0' character

ameyCU
  • 16,489
  • 2
  • 26
  • 41
fersarr
  • 3,399
  • 3
  • 28
  • 35
-1

Its checking if the end of string \0 NUL is reached or not, while copying the value of *t to *s and then incrementing both the pointers.

And to answer your 2nd question, consider this,

What is the difference between

if(a != 0)

&

if(a)

Its just two ways of writing the same code. The only difference that i can think of is code clarity. The first one is more verbose, its easier to read, understand and maintain.

Haris
  • 12,120
  • 6
  • 43
  • 70