main之前的函数调用。 [英] Function call before main.
问题描述
大家好,
我们可以将函数的返回值赋给全局变量吗?当我们知道
时,main()将是第一个要执行的函数。但如果上面的
为真,那么我们在main之前有一个函数调用。请帮助
我明白这个。代码可以是格式。
int f();
int x = f();
int main()
{
printf("%d",x);
}
>
int f()
{
x = 9;
}
在Turbo C ++编译器中,它给出x = 9;这怎么可能?
Srinu。
Hi all,
Can we assign return value of a function to a global variable? As we
know, main() will be the first function to be executed. but if the
above is true, then we have a function call before main. Please help
me calarifying this. The code may be of the form.
int f();
int x = f();
int main()
{
printf("%d", x);
}
int f()
{
x=9;
}
In Turbo C++ compiler, it gives x = 9; how is this possible?
Srinu.
推荐答案
Srinu写道:
Srinu wrote:
大家好,
我们可以将函数的返回值赋给全局变量吗?当我们知道
时,main()将是第一个要执行的函数。但如果上面的
为真,那么我们在main之前有一个函数调用。请帮助
我明白这个。代码可以是格式。
int f();
Hi all,
Can we assign return value of a function to a global variable? As we
know, main() will be the first function to be executed. but if the
above is true, then we have a function call before main. Please help
me calarifying this. The code may be of the form.
int f();
要明确说明函数不带参数,请使用`void`
关键字。
int f(void);
To state explicitly that the function takes no parameters use the `void`
keyword.
int f(void);
int x = f();
int main()
{
printf("%d",x);
int x = f();
int main()
{
printf("%d", x);
包括< stdio.hfor printf的原型。没有它,你是
调用未定义的行为。
Include <stdio.hfor the prototype for printf. Without it, you are
invoking undefined behaviour.
}
}
由于main被声明为返回一个int,因此返回一个值。使用0或
EXIT_SUCCESS进行成功终止,使用EXIT_FAILURE进行异常
终止。宏在< stdlib.h>中定义
Since main is declared as returning an int, return a value. Use 0 or
EXIT_SUCCESS for sucessful termination and EXIT_FAILURE for abnormal
termination. The macros are defined in <stdlib.h>
int f()
{
x = 9 ;
int f()
{
x=9;
f()被声明为返回一个int而你在这里什么都不返回。根据最新的C标准,这是不允许的b $ b,如果您尝试使用f()的返回值,可能导致不可预测的
行为,如您所做的那样
所以。
如果你不想让函数返回一个值,请指定:
void f ()...
f() is declared as returning an int and you return nothing here. This is
disallowed under the latest C Standard and can lead to unpredictable
behaviour if you attempt to use the return value of f(), as you''ve done
so.
If you don''t want a function to return a value specify:
void f() ...
}
在Turbo C ++编译器中,它给出x = 9;这怎么可能?
}
In Turbo C++ compiler, it gives x = 9; how is this possible?
纯粹运气。
By sheer luck.
Srinu写道:
Srinu wrote:
>
大家好,
我们可以将函数的返回值赋给全局变量吗?
>
Hi all,
Can we assign return value of a function to a global variable?
这是未定义的。
It''s undefined.
我们
知道, main()将是第一个要执行的函数。但如果上面的
为真,那么我们在main之前有一个函数调用。请帮助
我明白这个。代码可以是格式。
int f();
int x = f();
int main()
{
printf("%d",x);
}
>
int f()
{
x = 9;
}
在Turbo C ++编译器中,它给出x = 9;这怎么可能?
As we
know, main() will be the first function to be executed. but if the
above is true, then we have a function call before main. Please help
me calarifying this. The code may be of the form.
int f();
int x = f();
int main()
{
printf("%d", x);
}
int f()
{
x=9;
}
In Turbo C++ compiler, it gives x = 9; how is this possible?
代码未定义。
-
pete
The code is undefined.
--
pete
santosh< sa ********* @ gmail.comwrites:
santosh <sa*********@gmail.comwrites:
Srinu写道:
Srinu wrote:
>大家好,
我们可以将函数的返回值赋给全局变量吗?我们知道,main()将是第一个要执行的函数。但如果上面的
为真,那么我们在main之前有一个函数调用。请帮助我明白这个。代码可以是这种形式。
int f();
>Hi all,
Can we assign return value of a function to a global variable? As we
know, main() will be the first function to be executed. but if the
above is true, then we have a function call before main. Please help
me calarifying this. The code may be of the form.
int f();
要明确声明函数不带参数,请使用`void`
关键字。
To state explicitly that the function takes no parameters use the `void`
keyword.
什么是f()州?
What does "f()" state?
这篇关于main之前的函数调用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!