我需要专家建议以小时、分钟和秒为单位打印时间

I need expert advice to print the time in hours minutes and sec

nt main() {
    int hh,mm,ss;
    char time[2];
    printf("enter the time");
    scanf("%d %d %d %s",&hh,&mm,&ss,&time);
    printf("%d",ss);
    printf("hh:mm:ss time:%d:%d:%d %s",hh,mm,ss,time);
    //for(i=0;i<3;i++)
    //printf("%c",time[i]);
    return 0;
}

这个程序有什么问题,如果输入是任何非零整数,它每次都会打印 0

首先在发布问题时提供更多信息,而不仅仅是为什么这不起作用...

现在谈谈代码。

对于初学者 gcc 甚至不会让我编译这个,因为你在读取字符数组时不使用 & 符号.永远不要为字符数组分配少量内存,尤其是当您不知道它的大小时。

正确方法:

char time[100];
scanf(%s, time);

当我删除 & 符号并编译它时,它可以正常工作,但是我不知道你想要什么 char time[2]; 所以删除它,这是一个完美的工作(更优雅) 代码,但是请注意,如果您正在扫描整数,请不要向程序提供字符串!

#include <stdio.h>

int main() {

    int hh,mm,ss;
    printf("Enter the time: ");
    scanf("%d %d %d", &hh, &mm, &ss);
    printf("hh:mm:ss time: %d:%d:%d\n", hh, mm, ss);

    return 0;
}