C函数的声明在K&放大器; R [英] C functions declaration in K&R

查看:118
本文介绍了C函数的声明在K&放大器; R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉的K&安培; R风格的函数声明。

I'm not familiar with K&R style function declaration.

经过编译,以警告(只与返回的主要价值,过多使用 -Wall ),但什么是使用的变量的数据类型?

Following compiles, with warning (just related to return value of main that too with -Wall) but what are the data types of variables used ?

main(a, b, c, d){
    printf("%d", d);
}

foo(a, b){
     a = 2; 
     b = 'z';
}

如果这是一个问前请在评论部分的链接。我找不到类似的东西。

If this is a asked before please provide the link in comment section. I couldn't find something similar.

修改

我只是碰到一个模糊C code,它利用这些来了。结果
但是,我可以向你保证,我不会用C编程使用这样的语法。

I just came across an obfuscated C code, which uses these.
But I can assure you, I won't be using such syntax in C programming.

推荐答案

K&安培; R C是指由Kernighan的&放的1978年第一版定义的语言; Ritchie所著C程序设计语言。

"K&R C" refers to the language defined by the 1978 first edition of Kernighan & Ritchie's book "The C Programming Language".

在K&安培; R(即pre-ANSI)C,实体可以普遍地没有一个明确的类型声明,并且将默认为键入 INT 。这又回到到C的祖先语言,B和BCPL。

In K&R (i.e., pre-ANSI) C, entities could commonly be declared without an explicit type, and would default to type int. This goes back to C's ancestor languages, B and BCPL.

main(a,b,c,d){printf("%d", d);}

这是的等价于:

int main(int a, int b, int c, int d) {
    printf("%d", d);
}

旧语法在ANSI C(1989)和ISO C(1990)依然合法,但过时,但1999 ISO C标准放弃了隐式int类型的规则(同时保持旧式声明和定义语法)。

The old syntax remained legal but obsolescent in ANSI C (1989) and ISO C (1990), but the 1999 ISO C standard dropped the "implicit int" rule (while keeping the old-style declaration and definition syntax).

请注意,我说的是的的等价物。这是基本相同的定义,但作为一个声明它不提供参数类型信息。与旧式定义,用错号码或类型的参数调用不需要进行诊断;它只是未定义的行为。一个可见的原型,不匹配的参数触发编译时的诊断 - 和,如果可能的话,参数被隐式转换为参数类型

Note that I said it's nearly equivalent. It's essentially the same as a definition, but as a declaration it doesn't provide parameter type information. With the old-style definition, a call with the wrong number or types of arguments needn't be diagnosed; it's just undefined behavior. With a visible prototype, mismatched arguments trigger a compile-time diagnostic -- and, when possible, arguments are implicitly converted to the parameter type.

和,因为这是一个定义,还有另外一个问题:该标准只规定了两种形式的 (一个不带参数和一个有两个参数, ARGC 的argv )。实现可以支持其他的形式,而是一个有四个 INT 参数是不是可能是其中之一。该程序的行为,因此是不确定的。在实践中,很可能是 D 将对初始调用一些垃圾值。 (是的,一个递归调用允许在C,但几乎没有一个好主意。)

And since this is a definition of main, there's another problem: the standard only specifies two forms for main (one with no arguments and one with two arguments, argc and argv). An implementation may support other forms, but one with four int arguments isn't likely to be one of them. The program's behavior is therefore undefined. In practice, it's likely that d will have some garbage value on the initial call. (And yes, a recursive call to main is permitted in C, but hardly ever a good idea.)

foo(a,b){a=2; b='z';}

这几乎相当于:

int foo(int a, int b) {
    a = 2;
    b = 'z';
}

(请注意,以Z 的类型为 INT ,而不是类型的字符

(And note that z is of type int, not of type char.)

再次,旧表不给你的参数类型检查,所以喜欢一个电话:

And again, the old form doesn't give you parameter type checking, so a call like:

foo("wrong type and number of arguments", 1.5, &foo);

不需要进行诊断。

needn't be diagnosed.

底线:这是很好的知道如何K&安培; R风格函数声明和定义工作。目前仍然使用它们旧的code,而且他们仍然是合法的(但过时),即使在C2011(虽然没有隐式int类型的规则)。但非常接近没有很好的理由写code使用它们(除非您使用的是很旧的编译器卡住了,但是这是罕见的,越来越少了。)

The bottom line: It's good to know how K&R-style function declarations and definitions work. There's still old code that uses them, and they're still legal (but obsolescent) even in C2011 (though without the "implicit int" rule). But there is very nearly no good reason to write code that uses them (unless you're stuck using a very old compiler, but that's rare and becoming rarer.)

不过,我可以向你保证,我不会用C编程使用这样的语法。

But I can assure you, I won't be using such syntax in C programming.

出色!

这篇关于C函数的声明在K&放大器; R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆