小编CEA*_*EAC的帖子

Sprintf崩溃我的程序

我只是尝试使用sprintf连接一些字符串,但我有这个问题,我不明白为什么我的程序在C中使用sprintf崩溃.为什么这个代码运行?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* dateTime = malloc(16*sizeof(char));

    printf("Date: %s\n", __DATE__);
    printf("Time: %s\n", __TIME__);

    sprintf (dateTime, "%s, %s\0", __DATE__, __TIME__);

    printf("%s", dateTime);
    free(dateTime);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这不是吗?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* dateTime = malloc(16*sizeof(char));

    //printf("Date: %s\n", __DATE__);
    //printf("Time: %s\n", __TIME__);

    sprintf (dateTime, "%s, %s\0", __DATE__, __TIME__);

    printf("%s", dateTime);
    free(dateTime);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c printf pointers

3
推荐指数
1
解决办法
342
查看次数

标签 统计

c ×1

pointers ×1

printf ×1