The terminating NULL in an array in C

前端 未结 9 1126
走了就别回头了
走了就别回头了 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:47

    The purpose of null termination in strings is so that the parser knows when to stop iterating through the array of characters.

    So, when you use printf with the %s format character, it's essentially doing this:

    int i = 0;
    while(input[i] != '\0') {
       output(input[i]);
       i++;
    }
    

    This concept is commonly known as a sentinel.

提交回复
热议问题