[英]What happens when we assign a function call return value with return type void to a int variable?
#include<stdio.h>
int main()
{
//void foo();
int c= 5;
c=foo();
printf("\n%d",c);
return 0;
}
void foo()
{
printf("I am foo");
}
When I am commenting the prototype declaration then it gives the output: 当我评论原型声明时,它将给出输出:
I am foo
我是foo
8
8
With prototype it gives the error saying: 使用原型,它给出错误信息:
void value not ignored as it ought ought to be.
无效值不应该被忽略。
My question is, what is internally happening which result into the output when there is no prototype declaration? 我的问题是,当没有原型声明时,内部将发生什么结果输出? What is the reason behind it?
背后的原因是什么?
I am using Dev C++ editor with TDM-GCC 4.9.2 64bit compiler. 我正在将Dev C ++编辑器与TDM-GCC 4.9.2 64位编译器一起使用。
The sole reason this is built successfully is the implicit int rule (now removed in the latest revision of the C standard). 成功构建此文件的唯一原因是隐式int规则(已在C标准的最新版本中删除)。
The compiler sees no forward declaration of foo
, so it assumes that its return type is int. 编译器看不到
foo
前向声明,因此假定其返回类型为int。 It then proceeds to build and call your void foo()
. 然后,它将继续构建并调用您的
void foo()
。 After all is said and done, the behavior of your program is undefined. 说完一切后,程序的行为是不确定的。
Gcc compiler give warning and successfully run and return number of character with whitespace but some compiler give an error. Gcc编译器发出警告,并成功运行并返回带空格的字符数,但某些编译器给出错误。
pp.c:10:6: warning: conflicting types for ‘foo’ [enabled by default]
void foo()
^
pp.c:6:6: note: previous implicit declaration of ‘foo’ was here
c=foo();
when there is no prototype declaration
没有原型声明时
Well, that is invalid C. 好吧,那是无效的C。
To elaborate, 详细说明,
Point 1: You cannot have a function called which is not declared earlier. 要点1:不能有一个未提前声明的函数。 It is against the C standard rule.
这违反了C标准规则。 Note
注意
Point 2: As soon as you add the forward declaration, you'll be getting compilation error, as a void
type cannot be the RHS operand of an assignment operator and this is a constraint violation for assignment operator. 第2点:添加前向声明后,您将得到编译错误,因为
void
类型不能是赋值运算符的RHS操作数,并且这是赋值运算符的约束冲突。
Note: 注意:
Pre C99, it was allowed to have a function being called without the prototype known, it was assumed that the function will be returning an int
and will be accepting any number of parameters. 在C99之前的版本中,允许在不知道原型的情况下调用函数,并假定该函数将返回
int
并且将接受任何数量的参数。 However, this is precisely forbidden as per C99 and later, as the "implicit int rule" is removed from the standard. 但是,由于从标准中删除了“隐式int规则”,因此根据C99和更高版本,这是严格禁止的。 (Refer Foreword, paragraph 5, _"remove implicit int"_ in
C99
standard) . (请参阅前言,第5段,
C99
标准中的“删除隐式int” _) 。
In your case, for legacy reasons, the compiler still supports the implicit int rule, so it compiles fine, however, the assumption by compiler and the actual function definition is a mismatch. 在您的情况下,由于遗留原因,编译器仍支持隐式int规则,因此可以很好地进行编译,但是,编译器的假设与实际的函数定义不匹配。 This invokes undefined behavior .
这会调用未定义的行为 。
Quoting C11
, chapter §6.5.2.2, Function calls 引用
C11
,第6.5.2.2章, 函数调用
If the function is defined with a type that is not compatible with the type (of the expression) pointed to by the expression that denotes the called function, the behavior is undefined.
如果函数定义的类型与表示被调用函数的表达式所指向的类型(表达式的类型)不兼容,则行为是不确定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.