函数声明:K&R 与 ANSI

发布于 2024-09-06 21:21:26 字数 36 浏览 1 评论 0原文

K&R 函数声明和 ANSI 函数声明有什么区别?

What are the differences between a K&R function declaration and an ANSI function declaration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贩梦商人 2024-09-13 21:21:26

K&R 语法已过时,您可以跳过它,除非您必须维护非常旧的代码。

// K&R syntax
int foo(a, p) 
    int a; 
    char *p; 
{ 
    return 0; 
}

// ANSI syntax
int foo(int a, char *p) 
{ 
    return 0; 
}

K&R syntax is obsolete, you can skip it unless you have to maintain very old code.

// K&R syntax
int foo(a, p) 
    int a; 
    char *p; 
{ 
    return 0; 
}

// ANSI syntax
int foo(int a, char *p) 
{ 
    return 0; 
}
坐在坟头思考人生 2024-09-13 21:21:26

旧版 K&R 风格的声明/定义

当 Kernighan 和 Ritchie 首次出版《C 编程语言》时,C 尚未提供完整的函数原型。函数的前向声明是存在的,但其唯一目的是指示返回类型。对于返回 int 的函数,直到 C99 才需要它们。

到了 C89,添加了函数原型的概念,它还指定了参数的类型(以及隐式的参数数量)。由于原型也是函数声明的一种类型,因此非官方术语“K&R 函数声明”有时用于表示不是原型的函数声明。

// K&R declarations, we don't know whether these functions have parameters.
int foo(); // this declaration not strictly necessary until C99, because it returns int
float bar();

// Full prototypes, specifying the number and types of parameters
int foo(int);
float bar(int, float);

// K&R definition of a function
int foo(a)
    int a; // parameter types were declared separately
{
    // ...
    return 0;
}

// Modern definition of a function
float bar(int a, float b) 
{
    // ...
    return 0.0;
}

意外的 K&R 声明

值得注意的是,C 语言的新手在打算使用完整原型时可能会意外地使用 K&R 声明,因为他们可能没有意识到必须将空参数列表指定为无效

如果您将函数声明并定义为

// Accidental K&R declaration
int baz(); // May be called with any possible set of parameters

// Definition
int baz() // No actual parameters means undefined behavior if called with parameters.
          // Missing "void" in the parameter list of a definition is undesirable but not
          // strictly an error, no parameters in a definition does mean no parameters;
          // still, it's better to be in the habit of consistently using "void" for empty
          // parameter lists in C, so we don't forget when writing prototypes.
{
    // ...
    return 0;
}

...,那么您实际上并没有给出不带参数的函数的原型,而是以 K&R 风格声明了接受未知数量和未知类型的参数的函数。

AnT 在这个答案中指出类似的问题,这种语法已被弃用,但从 C99 开始仍然合法(并且该函数指针指向参数数量和类型未知的函数仍然具有潜在的应用,尽管存在未定义行为的高风险);因此,如果在没有正确原型的情况下声明或调用函数,兼容的编译器最多只会发出警告。

调用没有原型的函数不太安全,因为编译器无法验证您是否以正确的顺序传递了正确数量和类型的参数;如果调用实际上不正确,则会导致未定义的行为。

当然,声明和定义无参数函数的正确方法是:

// Modern declaration of a parameterless function.
int qux(void);  // "void" as a parameter type means there are no parameters.
                // Without using "void", this would be a K&R declaration.

// Modern definition of a parameterless function
int qux(void)
{
    // ...
    return 0;
}

Legacy K&R-Style Declarations/Definitions

When Kernighan and Ritchie first published "The C Programming Language", C didn't yet offer full function prototypes. Forward declarations of functions existed, but with the sole purpose of indicating a return type. For functions that returned int, they weren't required until C99.

By C89, the notion of a function prototype, which also specifies the types of the parameters (and, implicitly, their number) had been added. Since a prototype is also a type of function declaration, the unofficial term "K&R function declaration" is sometimes used for a function declaration that is not also a prototype.

// K&R declarations, we don't know whether these functions have parameters.
int foo(); // this declaration not strictly necessary until C99, because it returns int
float bar();

// Full prototypes, specifying the number and types of parameters
int foo(int);
float bar(int, float);

// K&R definition of a function
int foo(a)
    int a; // parameter types were declared separately
{
    // ...
    return 0;
}

// Modern definition of a function
float bar(int a, float b) 
{
    // ...
    return 0.0;
}

The Accidental K&R Declaration

It's worth noting that newcomers to C may accidentally use K&R declarations when they intend to use a full prototype, because they may not realize that an empty parameter list must be specified as void.

If you declare and define a function as

// Accidental K&R declaration
int baz(); // May be called with any possible set of parameters

// Definition
int baz() // No actual parameters means undefined behavior if called with parameters.
          // Missing "void" in the parameter list of a definition is undesirable but not
          // strictly an error, no parameters in a definition does mean no parameters;
          // still, it's better to be in the habit of consistently using "void" for empty
          // parameter lists in C, so we don't forget when writing prototypes.
{
    // ...
    return 0;
}

...then you have not actually given a prototype for a function that takes no parameters, but a declaration in K&R-style for a function that accepts an unknown number of parameters of unknown type.

AnT notes in this answer to a similar question that this syntax is deprecated but still legal as of C99 (and that function pointers to functions with unknown number and type of parameters still have potential applications, though at high risk of undefined behavior); as such, compliant compilers will, at best, produce a warning if a function is declared or called without a proper prototype.

Calling functions without prototypes is less safe, because the compiler cannot verify that you have passed the correct number and types of parameters in the correct order; undefined behavior results if the call is not actually correct.

The correct way to declare and define a parameterless function is, of course:

// Modern declaration of a parameterless function.
int qux(void);  // "void" as a parameter type means there are no parameters.
                // Without using "void", this would be a K&R declaration.

// Modern definition of a parameterless function
int qux(void)
{
    // ...
    return 0;
}
£冰雨忧蓝° 2024-09-13 21:21:26

我只是想在传统的 K & 中添加这一点。返回 int 值的函数的 R 风格类型修饰符甚至不是必需的。

考虑一个简单的 HelloWorld 程序的现代 C11 表示法:

int main(int argc, char **argv) {
    printf("hello world\n");
    return 0;
}

这相当于 K & R 表示法风格:

main(argc, argv)
int argc;
char **argv;
{
 printf("hello world\n");
 return 0;
}

请注意,main() 之前的 int 被忽略,但代码仍然可以编译。这是 K & 的一部分。 R 定义。

引用维基百科:

在 C 的早期版本中,只有在函数定义之前使用返回非 int 值的函数才需要声明;如果使用了其值,则假定在没有任何先前声明的情况下使用的函数返回 int 类型。

--source: https://en.wikipedia.org/wiki/C_( programming_language)#K.26R_C

这可以说是一种遗留的编码风格,由于清晰度问题应该避免,但旧的算法教科书通常更喜欢这种 K & R风格。

I just want to add that in the traditional K & R style type modifiers for functions that return an int value aren't even necessary.

Consider the modern C11 notation of a simple HelloWorld program:

int main(int argc, char **argv) {
    printf("hello world\n");
    return 0;
}

This is equivalent to the K & R notation style:

main(argc, argv)
int argc;
char **argv;
{
 printf("hello world\n");
 return 0;
}

Note that the int before main() is ignored, but the code still compiles. That's a part of the K & R definition.

Quote Wikipedia:

In early versions of C, only functions that returned a non-int value needed to be declared if used before the function definition; a function used without any previous declaration was assumed to return type int, if its value was used.

--source: https://en.wikipedia.org/wiki/C_(programming_language)#K.26R_C

This is arguably a legacy coding-style and should be avoided due to clarity issues, but quite often old algorithm textbooks favour this sort of K & R style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文