我正在尝试使用简单的calloc创建一个字符串,但我不明白为什么这段代码有效.这个calloc必须创建一个只有一个空格的字符串,只放一个char,对吧?为什么,当我尝试在stdout上打印我的字符串时,我看到值被转换为字符串?我认为stdout只出现了第一个数字,因为我已经用calloc分配了一个空格.
int main(int argc, const char* argv[]) {
char* string;
int value=6000031;
string=calloc(1,sizeof(char));
sprintf(string,"%d",value);
printf("%s\n",string);
free(string);
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)