C 我也这么做了;printf";,但结果不同

C 我也这么做了;printf";,但结果不同,c,C,这是简单的代码 它不应该打印:(空) 应该是:2017年7月31日星期一14:33:39 为什么要打印:(空) #包括“stdafx.h” #包括 #包括 #包括 int main() { 时间-当前时间; 字符c_时间_字符串[26]; currentTime=时间(空); 如果(当前时间==(时间)-1) { (void)fprintf(stderr,“无法获取当前时间。\n”); 退出(退出失败); } ctime_s(c_time_string,26和currentTime); if(c

这是简单的代码

它不应该打印:(空)

应该是:2017年7月31日星期一14:33:39

为什么要打印:(空)

#包括“stdafx.h”
#包括
#包括
#包括
int main()
{
时间-当前时间;
字符c_时间_字符串[26];
currentTime=时间(空);
如果(当前时间==(时间)-1)
{
(void)fprintf(stderr,“无法获取当前时间。\n”);
退出(退出失败);
}
ctime_s(c_time_string,26和currentTime);
if(c_time_string==NULL)
{
(void)fprintf(stderr,“无法获取当前时间。\n”);
退出(退出失败);
}
(void)printf(“a:%d\n a:%s\n”,currentTime,c\u time\u字符串);
(void)printf(“b:%s”,c_time_字符串);
getchar();
}

我解决了这个问题。见评论。有答案

以纯文本而不是图像的形式发布代码。请参阅以获取代码格式帮助。我怀疑
时间类型太大,不适合
%d
格式。因此您将得到null(
printf
尝试将
time\u t
的上半部分解释为字符串指针)。尝试使用
%ld
。在格式字符串中有两次
a:
。哪一个正在打印
(null)
?请注意
c\u time\u string
永远不能为null;它是一个本地数组。您应该检查来自的返回值,而不是查看数据。成功时返回0,失败时返回其他值
#include "stdafx.h"
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
    time_t currentTime;
    char c_time_string[26];


        currentTime = time(NULL);
        if (currentTime == (time_t)-1 )
        {
            (void)fprintf(stderr, "Failure to obtain the current time. \n");
            exit(EXIT_FAILURE);
        }
        ctime_s(c_time_string, 26, &currentTime);
        if ( c_time_string == NULL)
        {
            (void)fprintf(stderr, "Failure to obtain the current time. \n");
            exit(EXIT_FAILURE);
        }
        (void)printf("a: %d \n a:%s \n", currentTime, c_time_string);
        (void)printf("b: %s", c_time_string);
        getchar();
}