简体   繁体   English

为什么我们在 C 中需要终止字符

[英]Why do we need Termination Character In C

I am new to the C programming language and leaning it recent days.我是 C 编程语言的新手,最近几天开始学习它。 I am little confused on how termination character works and why do we need it.我对终止字符的工作原理以及我们为什么需要它有点困惑。 When I look at some documentation on the web, they usually state when we initialize the char array the last character must be '\0'.当我查看有关 web 的一些文档时,它们通常是 state,当我们初始化 char 数组时,最后一个字符必须是 '\0'。 However when I don't insert it to the end of char array it seems also compile and works well.但是,当我不将它插入到 char 数组的末尾时,它似乎也可以编译并且运行良好。 For example:例如:

char test[4] = "test";
printf("%s\n", test );
printf("%lu\n", strlen(test) );

it compiles, print out the value properly and also strlen return the correct value which means the compiler knows where is the last character in the char array.它编译,正确打印出值,并且 strlen 返回正确的值,这意味着编译器知道 char 数组中的最后一个字符在哪里。 Then why do we have to add '\0' at the end.那为什么还要在最后加上'\0'呢。 Is this JUST the convention?这只是惯例吗? Or there is something else?还是有别的东西?

Thanks.谢谢。

You explictely declared test as an array of four chars, therefore it is an array of four chars, containing 't', 'e', 's', and 't'.您明确将 test 声明为一个由四个字符组成的数组,因此它是一个由四个字符组成的数组,包含“t”、“e”、“s”和“t”。

printf ("%s") expects a zero-terminated string, and so does strlen. printf ("%s") 需要一个以零结尾的字符串,strlen 也是如此。 You have an array of four characters, not containing a zero, so passing this to printf or strlen is undefined behaviour.您有一个包含四个字符的数组,不包含零,因此将其传递给 printf 或 strlen 是未定义的行为。

Since your array is followed by unknown bytes, it may be by pure coincidence that the next byte is a zero.由于您的数组后面是未知字节,因此下一个字节为零可能纯属巧合。 And if that is the case, then it may be that "test" is printed and strlen() returns 4. But that is pure coincidence.如果是这种情况,那么可能是打印了“test”并且 strlen() 返回 4。但这纯属巧合。

What can also happen is that your program crashes.还可能发生的是您的程序崩溃。 Or that it prints "testgarbagegarbagegarbage" and strlen returns some large number.或者它打印“testgarbagegarbagegarbage”并且strlen返回一些大数字。 Or that your program happily works as expected while you are developing it, and that it crashes when the first paying customer is using it.或者您的程序在开发过程中按预期愉快地工作,并且当第一个付费客户使用它时它会崩溃。

Your code has undefined behaviour, which just means anything can happen.您的代码具有未定义的行为,这意味着任何事情都可能发生。 You fix this by declaring char test[] = "test";你可以通过声明 char test[] = "test"; 来解决这个问题。 which will make test[] large enough to hold four chars and a zero byte.这将使 test[] 大到足以容纳四个字符和一个零字节。

The compiler knows the length of statically-declared arrays.编译器知道静态声明的 arrays 的长度。 But C is more flexible than that, it allows you to use more general pointers.但是 C 比这更灵活,它允许您使用更通用的指针。 When you're accessing an array through a pointer, there's no way for the compiler to know which array it points to.当您通过指针访问数组时,编译器无法知道它指向哪个数组。 In particular, when the parameter of a function is a string, it could be any string in the program.特别是当function的参数为字符串时,可以是程序中的任意字符串。

Consider the following code:考虑以下代码:

char test1[4] = "test";
char test2[8] = "12345678";
char *test = (rand() % 2 == 0) ? test1 : test2;
printf("%s\n", test);

There's no way for the compiler to know the length of test -- depending on the random choice it could be 4 or 8.编译器无法知道test的长度——取决于随机选择,它可能是 4 或 8。

You also can't simply store the length at the beginning of the string, because C allows you to make pointers to any array element.您也不能简单地将长度存储在字符串的开头,因为 C 允许您创建指向任何数组元素的指针。 Consider:考虑:

char test1[8] = "12345678";
char *test = &test[rand() % 8];
printf("%s\n", test);

There are other languages that solve this problem using indirection.还有其他语言使用间接来解决这个问题。 For instance, C++ has the std::string class, which uses a structure that contains the length and a pointer to the string contents.例如,C++ 具有std::string class,它使用包含长度和指向字符串内容的指针的结构。 When you create substrings it allocates a new structure and copies the data.当您创建子字符串时,它会分配一个新结构并复制数据。 But C is a simpler language, and is designed to allow more direct memory access.但是 C 是一种更简单的语言,旨在允许更直接的 memory 访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM