简体   繁体   English

当我们将返回类型为void的函数调用返回值分配给int变量时会发生什么?

[英]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.

相关问题 当您调用带有返回值的函数而不将其分配给任何变量时会发生什么? - What happens when you call a function with return value without assigning it to any variable? 将函数的返回类型存储在其他类型的变量中时会发生什么? - What happens when storing a function's return type in a variable of a different type? 如果main()没有返回int值会发生什么? - What happens if main() does not return an int value? 当我们写 int x 时会发生什么; 并从 C 语言的主 function 返回操作系统是否为它分配 memory? - What happens when we write int x; and return from the main function in C language does OS allocate memory for it? 当我们将一个 int 分配给一个由 2 个 int 组成的数组时会发生什么? - What happens when we assign one int to an array of 2 ints? 我们可以在例程中调用函数,该例程的函数返回数据类型为void吗? - Can we call a function inside a routine where the function return data type is void? 如果我们为变量分配一个新值,旧值会发生什么? (在 C 中) - If we assign a new value to a variable, what happens to the old value ? (in C) 将值分配给 function,为什么我们无法在 function 返回值时将值分配给它,但在引用和指针的情况下它可以工作? - Assign Value to function,why we are not able to assign the value to function when it return the value but in case of reference and pointers it works? 当返回值的类型与声明的返回类型不同时,返回值会怎样? - What happens to return value when its type differs from the declared return type? “const int x = get();”在C中合法吗?我们可以在声明时将函数的返回值赋给常量吗? - Is “const int x = get();” legal in C?Can we assign a function's return value to a constant at declaration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM