How does this C copy function exit its loop?

后端 未结 3 1552
心在旅途
心在旅途 2021-01-21 03:14
void copy (char *source, char *dest) {
    while (*dest++ = *source++);
}

The char that is represented by *source is copied to the field <

3条回答
  •  爱一瞬间的悲伤
    2021-01-21 03:28

    chars are integral types. Integral types are interpreted as conditionals in the following way:

     0 -> false
     Anything else -> true
    

    Since "strings" in C are null-terminated (meaning 0 or '\0') when it reaches the end of the string it stops.

提交回复
热议问题