简体   繁体   English

为什么这个程序没有显示任何错误?

[英]why this program is not showing any error?

in the code below . 在下面的代码中。

  • i have defined function prototype with no argument 我已经定义了没有参数的函数原型
  • in definition as well as in function call i have used one parameter. 在定义以及函数调用中,我都使用了一个参数。

i would like to know why i am not getting any error ? 我想知道为什么我没有任何错误?

# include <stdio.h>
float circle();       /* no parameter*/
int main()
{
    float area;
    int radius =2;
    area=circle(radius);
    printf("%f \n",area);
    return 0;
}

float circle( r) /* with one parameter even no parameter type */
{
    float a;
    a=3.14*r*r;
    return (a);
}

The

float circle();

is not a function with zero parameters. 不是零参数的函数。 It's a function with an unspecified number of parameters. 这是一个参数数量不确定的函数。

The

float circle( r) {

is a K&R-style definition in which the type of r defaults to int . 是K&R样式的定义,其中r的类型默认为int See https://stackoverflow.com/a/18433812/367273 参见https://stackoverflow.com/a/18433812/367273

This is because compiler treat r as int by default when no parameter is defined for circle . 这是因为,当没有为circle定义参数时,编译器默认将r视为int Try to run your code after declaring function prototype as 在声明函数原型为后尝试运行代码

float circle(void);  

and you will get error. 你会得到错误。

That's because function 那是因为功能

float circle();

declaration doesn't declare function that takes no arguments. 声明不声明不带参数的函数。
It's implicitly declared as a function that takes undefined number of integer variables as arguments. 它隐式声明为一个函数,该函数使用未定义数量的整数变量作为参数。
Just like 就像

function();

is valid function declaration. 是有效的函数声明。 Implicitly this function will be treated as function taking int as arguments and returning int . 隐式地,此函数将被视为以int作为参数并返回int函数。
If you want to declare function function taking no arguments or not returning any value, you do it with void keyword: 如果要声明不带任何参数或不返回任何值的函数,可以使用void关键字:

void funct(void);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个简单的 C 程序没有显示任何输出? - Why is this simple C program not showing any output? 为什么这个程序没有显示3以上的任何数字? - Why isn't this program showing any number above 3? 为什么下面程序的output出现编译时错误? - Why is the output of the following program showing compile time error? pipe - fork - c - 为什么程序无响应且没有任何错误? - pipe - fork - c - why the program is unresponsive without any error? 为什么我的程序会突然跳出而没有任何错误? - Why is my program snapping out abruptly without any error? 为什么我的程序在没有任何错误信息的情况下在waitpid()中停止? - Why my program stops in waitpid() without any error information? 为什么我的计算均方误差的程序没有给出任何 output? - Why is my program to calculate Mean square error not giving any output? 为什么我的程序没有显示任何 output? 0 个错误,0 个警告但没有 output? 我正在使用 Dev C++ 编译器 - Why is my program not showing any output? 0 errors, 0 warnings but no output? I'm using Dev C++ compiler c中的双星号。为什么在下面的程序中不使用它时显示错误? - Double asterstik in c. Why its showing error when I dont use it in this following program? 为什么以及如何,这个C程序准确显示7.21? - Why and How, is this C program showing 7.21 accurately?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM