【发布时间】:2016-07-21 18:35:19
【问题描述】:
我正在编写一个程序来计算两个给定时间之间经过的时间。
由于某种原因,我收到错误:在我的主函数之前我的 elapsedTime 函数原型的预期标识符或“C”。
我已经尝试在程序中移动它,如果在声明 t1 和 t2 之后找到它并没有什么不同。有什么问题?
谢谢
#include <stdio.h>
struct time
{
int seconds;
int minutes;
int hours;
};
struct elapsedTime(struct time t1, struct time t2);
int main(void)
{
struct time t1, t2;
printf("Enter start time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d:%d:%d", &t1.hours, &t1.minutes, &t1.seconds);
printf("Enter stop time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d:%d:%d", &t2.hours, &t2.minutes, &t2.seconds);
elapsedTime(t1, t2);
printf("\nTIME DIFFERENCE: %d:%d:%d -> ", t1.hours, t1.minutes, t1.seconds);
printf("%d:%d:%d ", t2.hours, t2.minutes, t2.seconds);
printf("= %d:%d:%d\n", differ.hours, differ.minutes, differ.seconds);
return 0;
}
struct elapsedTime(struct time t1, struct time t2)
{
struct time differ;
if(t2.seconds > t1.seconds)
{
--t1.minutes;
t1.seconds += 60;
}
differ.seconds = t2.seconds - t1.seconds;
if(t2.minutes > t1.minutes)
{
--t1.hours;
t1.minutes += 60;
}
differ.minutes = t2.minutes - t1.minutes;
differ.hours = t2.hours - t1.hours;
return differ;
}
【问题讨论】:
-
那是什么:
struct elapsedTime(struct time t1, struct time t2);?struct不是有效类型。 -
@EugeneSh.:这是一个函数声明。
struct关键字后面的标签time缺失。 -
为了将来参考,您应该将确切的错误消息复制并粘贴到您的问题中,包括它所引用的行号。
-
会做的,谢谢大家