What is the difference between (char)0 and '\0'? in C

前端 未结 4 831
梦谈多话
梦谈多话 2020-12-24 12:23

What is the difference between using (char)0 and \'\\0\' to denote the terminating null character in a character array?

4条回答
  •  天涯浪人
    2020-12-24 13:01

    Zero can mean a lot of different things in C. You should use the null character '\0', because then there can be no confusion over that your intention is string termination.

    If you set a char to 0, that can mean null termination, but it can also mean that you are just using the char as an 8-bit integer for calculations, rather than as part of a string. This can be confused further if you are also using pointers in the same code and compare them against zero, which is then a null pointer.

提交回复
热议问题