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
  • 24,988
  • 16
  • 102
  • 110
Mod Mark
  • 199
  • 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
  • 380,411
  • 33
  • 383
  • 585
-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,146
  • 2
  • 24
  • 40
fersarr
  • 3,289
  • 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
  • 11,989
  • 6
  • 41
  • 67