The terminating NULL in an array in C

前端 未结 9 1007
走了就别回头了
走了就别回头了 2021-01-21 04:15

I have a simple question.
Why is it necessary to consider the terminating null in an array of chars (or simply a string) and not in an array of integers. So when i want a

9条回答
  •  时光说笑
    2021-01-21 04:39

    Null terminators are required at the end of strings (or character arrays) because:

    1. Most standard library string functions expect the null character to be there. It's put there in lieu of passing an explicit string length (though some functions require that instead.)
    2. By design, the NUL character (ASCII 0x00) is used to designate the end of strings. Hence why it's also used as an EOF character when reading from ASCII files or streams.

    Technically, if you're doing your own string manipulation with your own coded functions, you don't need a null terminator; you just need to keep track of how long the string is. But, if you use just about anything standardized, it will expect it.

提交回复
热议问题