年数 [英] Number of Years
问题描述
G''day
我正在努力计算自1970年以来的年数,这是我的代码:
包括< stdio.h>
#include< time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
int secToYear(int seconds) //功能原型
{
秒=(SEC_IN_YEAR);
返回(秒);
}
main(){
time_t now = time(0);
printf("%i seconds from 1/1 / 1970 \ n",secToYear(now));
}
出于某种原因,答案是1.有人可以给我
一些指导。
谢谢你
格雷格
" Gregc"。写道:G''day
我正在努力计算自1970年以来的年数,这里是我的
代码:
包括< stdio.h>
你错过了#here。
#include< time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
最终为31536000,不适合int。
请使用long。
为什么不只需:
const long SEC_IN_YEAR = 365 * 24 * 60 * 60;
因为这是你使用的唯一常量。 />
int secToYear(int seconds)//函数原型
这不是原型。这是实施。
{
秒=(SEC_IN_YEAR);
返回(秒);
好的......你拿参数秒,分配一个常数并返回
... ...
我不知道你打算在这里做什么。我想一些
计算...
}
main(){
int main ()将是正确的方法,因为标准要求main()
返回一个int。
time_t now = time(0);
printf(" ;%i秒自1/1/1970 \ n",secToYear(现在));
返回EXIT_SUCCESS;
}
由于某种原因,答案是1.可以有人提供给我
一些指导。
谢谢你
Greg
问候
John
" Gregc。写道:
我正在努力计算自1970年以来的年数,这是我的代码:
include< stdio.h>
#include< time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
int secToYear(int seconds)//函数原型
{
秒=(SEC_IN_YEAR);
您忽略了传递给函数的参数。你只是简单地忽略你在调用者中提供的now的值,并覆盖
它。我怀疑还有别的东西,但这是我首先要注意的事情。
返回(秒);
}
main(){
time_t now = time(0);
printf("%i seconds from 1/1 / 1970\\\
,secToYear(now));
}
由于某种原因,答案是1.有人可以给我一些指导。
谢谢你
> Greg
感谢您的帮助。我想要制作的是36年,即2006年至1970年的b $ b。
格雷格
John F写道:Gregc。写道:G''day
我正在努力计算自1970年以来的年数,这里是我的
代码:
包括< stdio.h>
你错过了#here。
#include< time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
这最终为31536000,不适合int。
使用long。
为什么不只是:
const long SEC_IN_YEAR = 365 * 24 * 60 * 60;
因为这是你使用的唯一常数。
int secToYear(int秒)//函数原型
这不是原型。这是实现。
{
秒=(SEC_IN_YEAR);
返回(秒);
好的...你拿参数秒,分配一个常数并返回那个......?
我不清楚你打算在这里做什么。我想一些
计算......
}
main(){
int main ()将是正确的方法,因为标准要求main()
返回一个int。
time_t now = time(0);
printf(" ;%i秒自1/1 / 1970 \ n",secToYear(now));
返回EXIT_SUCCESS;
}
由于某种原因,答案是1.可以有人提供
我的
一些指导。
<谢谢你
Greg
问候
John
>
G''day
I''m trying to work out the number of years since 1970, here is my code:
include <stdio.h>
#include <time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
int secToYear(int seconds) //Function Protoype
{
seconds = (SEC_IN_YEAR);
return (seconds);
}
main () {
time_t now = time(0);
printf("%i seconds since 1/1/1970\n",secToYear(now));
}
For some reason the answer is coming out as 1. Could someone offer me
some guidance.
Thankyou
Greg
"Gregc." wrote:G''day
I''m trying to work out the number of years since 1970, here is my
code:
include <stdio.h>
You missed # here.
#include <time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
This ends up as 31536000 which does not fit an int.
Use long instead.
why not just:
const long SEC_IN_YEAR=365*24*60*60;
since this is the only constant you are using.
int secToYear(int seconds) //Function Protoype
This is not a prototype. It is the implementation.
{
seconds = (SEC_IN_YEAR);
return (seconds);
OK... you take the parameter "seconds", assign a constant and return
that...?
I have no clue about what you intend to do here. I suppose some
calculations...
}
main () {
int main() would be the correct way since the standard requires main()
to return an int.
time_t now = time(0);
printf("%i seconds since 1/1/1970\n",secToYear(now));
return EXIT_SUCCESS;
}
For some reason the answer is coming out as 1. Could someone offer
me
some guidance.
Thankyou
Greg
regards
John
"Gregc." writes:
I''m trying to work out the number of years since 1970, here is my code:
include <stdio.h>
#include <time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
int secToYear(int seconds) //Function Protoype
{
seconds = (SEC_IN_YEAR);
You are ignoring the parameter passed to the function,. You are simply
ignoring the value of now, which you provided in the caller, and overwriting
it. I suspect there is something else too, but this is the first thing I
notice.
return (seconds);
}
main () {
time_t now = time(0);
printf("%i seconds since 1/1/1970\n",secToYear(now));
}
For some reason the answer is coming out as 1. Could someone offer me
some guidance.
Thankyou
Greg
Thanks for your help. What I am trying to produce is 36 years ie
2006-1970.
Greg
John F wrote:"Gregc." wrote:G''day
I''m trying to work out the number of years since 1970, here is my
code:
include <stdio.h>
You missed # here.#include <time.h>
const int SEC_IN_MIN = 60;
const int SEC_IN_HOUR = SEC_IN_MIN * 60;
const int SEC_IN_DAY = SEC_IN_HOUR * 24;
const int SEC_IN_YEAR = SEC_IN_DAY * 365;
This ends up as 31536000 which does not fit an int.
Use long instead.
why not just:
const long SEC_IN_YEAR=365*24*60*60;
since this is the only constant you are using.
int secToYear(int seconds) //Function Protoype
This is not a prototype. It is the implementation.{
seconds = (SEC_IN_YEAR);
return (seconds);
OK... you take the parameter "seconds", assign a constant and return
that...?
I have no clue about what you intend to do here. I suppose some
calculations...}
main () {
int main() would be the correct way since the standard requires main()
to return an int.time_t now = time(0);
printf("%i seconds since 1/1/1970\n",secToYear(now));
return EXIT_SUCCESS;}
For some reason the answer is coming out as 1. Could someone offer
me
some guidance.
Thankyou
Greg
regards
John
这篇关于年数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!