#include<stdio.h>
#include<string.h>
void terminateString(char *str){
str[3] = 0;
printf("string after termination is:%s\n",str);
}
int main(){
char str[]="abababcdfef";
terminateString(str);
return 0;
}
输出:
string after termination is:aba
我们仅将索引“3”处的元素分配给 0,但为什么该索引之后的所有字符都被忽略?有人可以解释一下这种行为吗?
最佳答案
We are only assigning element at index '3' to 0, but why do all characters after that index are ignored? Can someone please explain this behavior?
以零结尾的字符串的约定是 0 字节表示字符串的结尾。因此,当 printf() 在位置 3 遇到零字节时,它会停止打印。
关于c - 字符串终止 C/C++ char = 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45604770/