2

There are two versions of string copy functions written in C. My question is why the version1 need "!= '\0'" but the version2 doesn't. What if I have a character 0 to be copied using version2, will the '0' terminate coping process?

void version1(char to[], char from[])
{
    int i;

    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

char *version2(char *dest, const char *src)
{
    char *addr = dest;
    while (*dest++ = *src++);
    return addr;
}

In addition, why an input like "1230456" will not terminate the coping since '0' appears in the middle of the string?

1
  • I don't see any function not using pointers. Also, where a logical expression is required, nonzero integers are considered true, zero is false, so *dest++ = *src++ is the same as (*dest++ = *src++) != 0. Read a C book. Commented Nov 12, 2013 at 18:46

5 Answers 5

6

This is because in C comparison to zero is optional. When you use an expression in a context requiring a logical expression, C would insert an implicit comparison to zero for you.

You can rewrite the first function as follows without changing the semantic:

while ((to[i] = from[i]))
    ++i;

Moreover, you can rewrite the second function as follows:

while ((*dest++ = *src++) != '\0');
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your answer. I just add an example. Why can I get the whole string but not the half if I try to copy a string like "1230456"?
@wuyefeibao You are confusing '0' - the ASCII symbol for zero, and '\0' (with a slash) - the null terminator; they are not the same! The "1230456" contains the first; the strcpy looks for the second.
0

There is exactly that same != 0 test in the second version, but it's impicit: The result of the expression *dest++ = *src++ becomes the value checked by the while, and in C, all tests boil down to a comparison with zero.

By the same token, in the first example, the while line could be rewritten:

while (to[i] = from[i])

and not change the meaning.

Comments

0

Both versions make the same check, in the second version you just don't see it. You can try to remove != '\0' from the first version, it should still work.

Comments

0

version1 does not NEED the != '\0' but it is better programming practice to include it. It just so happens that '\0' is equal to zero and so version2 will work, but, if you happen to come across a system where '\0' is NOT zero, version2 will not work.

1 Comment

I think the standard dictates \0 be zero.
0

One thing perhaps not mentioned yet is that the reason the zero termination gets copied in the second example is because of the post increment ensures that the value is copied BEFORE it gets checked for a zero value, which terminates the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.