简体   繁体   English

这个奇怪的函数定义意味着什么?

[英]What does this weird function definition mean?

I came across this C program in a blog post : 我在博客文章中遇到了这个C程序:

main()
{
    int n;
    n = 151;
    f(n);
}

f(x)
int x;
{
    printf("%d.\n", x);
}

The post doesn't explain it. 这篇文章没有解释。 Can anybody explain what this weird function definition means? 任何人都可以解释这个奇怪的函数定义意味着什么吗?

This is the K&R style of C code, it's still valid C syntax, but I suggest you not using it in new-written code. 这是K&R风格的C代码,它仍然是有效的C语法,但我建议你不要在新编写的代码中使用它。

f(x)
int x;

is equivalent to ANSI C: 相当于ANSI C:

void f(int x)

K&R C is the C language described in the first edition of the book C programming language by Brian Kernighan and Dennis Ritchie, and named after the two authors. K&R C是Brian Kernighan和Dennis Ritchie在C编程语言第一版中描述的C语言,并以两位作者的名字命名。 The second edition of the book updated to use ANSI C. 本书的第二版更新为使用ANSI C.

f(x)
int x;
{
    printf("%d.\n", x);
}

is an older way of defining function. 是一种定义功能的旧方法。 Now it can be read as 现在它可以被解读为

void f(int x)
{
    printf("%d.\n", x);
}

This code is simply bad. 这段代码很糟糕。

  1. Obsolete declaration of function f() 函数f()过时声明
  2. There should be forward declaration of f() before main() main()之前应该有f()前向声明
  3. Return type is missing for f() f()缺少返回类型

please change your c learning source! 请更改您的c学习资源!

that is a very old style of C (K&R C). 这是一种非常古老的C风格(K&R C)。

f(x)

int x;
{}

is equivalent to 相当于

void f(int x)
{}

you should really not wasting time learning that. 你真的不应该浪费时间去学习。

look for sources that teach ANSI C C89/C90 and also note the new features of C99 (that isn't widely adopted by many compilers, so know the differences) 寻找教授ANSI C C89 / C90的资料,并注意C99的新功能(许多编译器没有广泛采用,所以要知道差异)

This is a very old style of coding. 这是一种非常古老的编码风格。 This doesn't work out in ANSI. 这在ANSI中不起作用。 Better use something like 更好地使用像

void f(int x)
{
 ... ... ...;/*Whatever is required*/
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM