C中的第一个程序 - 函数返回的内容是什么? [英] First program in C - what is going on with function returns?

查看:69
本文介绍了C中的第一个程序 - 函数返回的内容是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我刚刚编写了一个简单的程序让我开始使用C /

计算自出生日期以来的天数。 />
让我对程序感到困惑的一件事(即使它有效)是

全局变量和函数返回的工作方式......


例如,我有一个全局数组char datestring [80];这是函数speakdate中定义的
。 speakdate只是将一组

整数(日期变量)转换为字符串。


然后主程序执行:

" ; printf(当前日期是:

%s\\\
\ n,发言权(cdate,cmonth,cyear));"


并正确打印日期字符串的值。

我没有得到的是为什么这有效,因为我没有明确告诉

speakdate返回日期字符串。

我认为默认情况下它根本不会返回任何内容。


完整代码如下。顺便说一下,我的代码上的任何其他提示都会受到欢迎

并赞赏!


干杯,


Ben C

Hi,

I have just written a simple program to get me started in C that
calculates the number of days since your birthdate.
One thing that confuses me about the program (even though it works) is
how global variables and function returns work...

For example, I have a global array "char datestring[80];" which is
defined in the function speakdate. speakdate just converts a set of
integers (date variables) to a string.

The main program then does:
"printf ("The current date is:
%s\n\n",speakdate(cdate,cmonth,cyear));"

And correctly prints the value of datestring.
What I don''t get is why this works since I haven''t explicitly told
speakdate to return datestring.
I figured by default it would return nothing at all.

Full code below. By the way any other tips on my code would be welcomed
and appreciated!

cheers,

Ben C

展开 | 选择 | Wrap | 行号

推荐答案


是********* @ gmail.com 写道:

<我刚刚编写了一个简单的程序,让我开始使用C语言来计算自出生日期以来的天数。
让我对程序感到困惑的一件事(即使它有效)是< BR全局变量和函数返回的工作方式......

例如,我有一个全局数组char datestring [80];
在函数speakdate中定义。 speakdate只是将一组
整数(日期变量)转换为字符串。

然后主程序执行:
" printf("当前日期是:
%s \ n \ n",speakdate(cdate,cmonth,cyear));"

并正确打印日期字符串的值。
我不知道的是什么得到是为什么这是有效的,因为我没有明确告诉
speakdate返回日期字符串。
我认为默认情况下它什么都不返回。
Hi,

I have just written a simple program to get me started in C that
calculates the number of days since your birthdate.
One thing that confuses me about the program (even though it works) is
how global variables and function returns work...

For example, I have a global array "char datestring[80];" which is
defined in the function speakdate. speakdate just converts a set of
integers (date variables) to a string.

The main program then does:
"printf ("The current date is:
%s\n\n",speakdate(cdate,cmonth,cyear));"

And correctly prints the value of datestring.
What I don''t get is why this works since I haven''t explicitly told
speakdate to return datestring.
I figured by default it would return nothing at all.




默认情况下,它返回一个int。我不确定,但我相信

正在发生的是最后一个strcat的返回值是从speakdate返回

。 strcat返回一个char *,而char *是
强制转换为int。你的程序正在工作,因为在你的机器上int

和char *可能有相同的大小。


为了避免这个问题,你应该明确指出返回类型

函数,并明确返回一个值。



By default, it returns an int. I''m not certain, but I believe what is
happening is that the return value of the last strcat is being returned
from speakdate. strcat is returning a char *, and the char * is being
cast to an int. You''re program is working because on your machine int
and char * probably have the same size.

To avoid this problem, you should explicitely specify the return type
of the function, and explicitely return a value.


[snips]

2006年4月29日星期六23:52:57 -0700,ben.carbery写道:
[snips]

On Sat, 29 Apr 2006 23:52:57 -0700, ben.carbery wrote:
下面的完整代码。顺便说一下,我的代码上的任何其他提示都会受到欢迎
和赞赏!



您说的是完整代码 - 但我在这里看不到合适的内容。

char datestring [80];
/ * dim是''几个月的日子''* /
int dim ;

speakdate(int d,int m,int y)


返回什么类型?如果你的意思是没有,请指定void。

strcpy(datestring,stdate);
strcat(datestring,stmonth);


在函数末尾,全局日期字符串有一个日期。到目前为止

好​​。


int countdays(int bd,int bm,int by,int cd,int cm,int cy){

int月,年;

int daysalive = 0;

/ *计算全年的天数* /
(年= +1;年< cy;年= ++年){


你可能不想年= ++年,但只是++年

if(fmod(year,4)== 0){


猜测,你只需要模数操作数 - % - 这里。

main(void)


int main(无效)

{

int bdate,bmonth,byear;

printf("请输入您的出生日期(d / m / yyyy):\ n"); scanf
(%d /%d /%d,& bdate,& bmonth,& byear);
printf(你的出生日期是%s \ n \ nn,发言日期(bdate,bmonth,byear));


speakdate(),如所写的那样,什么都不返回,所以将它视为好像是

一个错误。你得到任何输出都只是一个意外问题。

printf(恭喜!你出生时总共有%d天
之前。\ nn,计算日( bdate,bmonth,byear,cdate,cmonth,cyear));

[/ code]
Full code below. By the way any other tips on my code would be welcomed
and appreciated!

You said "full code" - but I don''t see the proper includes here.
char datestring[80];
/* dim is ''days in month''*/
int dim;

speakdate(int d, int m, int y)
What type is being returned? If you meant "nothing", specify void.
strcpy(datestring,stdate);
strcat(datestring,stmonth);
At the end of the function, the global datestring has a date. So far so
good.

int countdays(int bd, int bm, int by, int cd, int cm, int cy) {

int month,year;

int daysalive = 0;

/* count the days in whole years */
for (year=by+1;year<cy;year=++year) {
You probably don''t want year=++year, but simply ++year
if (fmod(year,4)==0) {
At a guess, you simply want the modulus operand - % - here.
main(void)
int main(void)
{

int bdate,bmonth,byear;

printf ("Please enter your birth date (d/m/yyyy):\n"); scanf
("%d/%d/%d",&bdate,&bmonth,&byear); printf ("Your date of birth is %s\n\n",speakdate(bdate,bmonth,byear));
speakdate(), as written, returns nothing, so treating it as if it does is
an error. That you get any output at all is simply a matter of accident.
printf ("Congratulations! You were born a total of %d days
ago.\n",countdays(bdate,bmonth,byear,cdate,cmonth, cyear));
[/code]




没有近距离,没有回报。尽管声称下面的完整代码,

但是这不可能是完整的代码,因为它无法编译。


那个说,你还应该把编译器的警告级别调到最大,

并注意警告。



No close brace, no return. Despite the claim of "full code below",
there''s no way this could be the full code, as it cannot compile.

That said, you should also turn your compiler''s warning level to maximum,
and pay attention to the warnings.


我想我做了这是因为我无法弄清楚如何返回一个字符串。


" char speakdate(int d,int m,int y){"工作正常,但是


" char [whatever] speakdate(int d,int m,int y){"没有。


这里有另外的语法吗?

I think I did this since I couldn''t figure out how to return a string.

"char speakdate(int d, int m, int y) {" works ok, but

"char[whatever] speakdate(int d, int m, int y) {" didn''t.

Is there another syntax to use here?


这篇关于C中的第一个程序 - 函数返回的内容是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆