标识符列表Vs C中的参数类型列表 [英] Identifier list Vs Parameter type list in C

查看:83
本文介绍了标识符列表Vs C中的参数类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  6.7.6.3函数声明符(包括原型)

本部分涉及'标识符列表''参数类型列表'

首先,函数声明(不定义)与函数原型相同。我对么?如果这是正确的,那么为什么标准说<'包括原型'?



我无法理解差异在'标识符列表''参数类型列表'之间关于函数声明

  int fun(); //声明
int fun(int x)//定义,但签名不匹配并且工作正常。
{return x; }

有人可以解释一下,我很困惑吗?

解决方案

函数声明与函数原型不一样。原型是一种特殊的声明。例如,这是一个函数声明,它不是原型

  int foo(); 

以下声明是原型

  int foo(int a,double b); 
int bar(char,float);
float baz(void);

I。原型是描述功能参数的数量和类型的声明。一个非原型声明没有提及任何有关参数的内容。



现在,除了现代函数定义之外,C语言还支持旧的K& R风格函数定义。 K& R风格的函数定义如下所示:

  int foo(a,b)
int a;
double b;
{
...
}

函数定义如下所示:

  int foo(int a,double b)
{
...

$ / code>

正如您所看到的,K& R样式参数列表只是 a,b 。它包含参数名称,但不包括它们的类型。这就是语法所指的 identifier-list 。 现代参数列表是 int a,double b ,这就是语法所指的 parameter-type-list

identifier-list 是K& R风格函数定义语法的一部分,而 parameter-type-list 是现代函数定义语法的一部分。 >

还要注意的是,在C语言中声明

  int foo(); 

并不意味着 foo 不带任何参数。这意味着 foo 会带一个未指定的个参数,也就是说它只关闭参数类型检查,参数数量检查和 foo 。这样的签名将与任何参数列表匹配 foo 的定义。这

  int foo(int x)
{
...
}

对于 foo 而言是一个完美有效的定义,如图所示以上。使用()声明它的事实仅仅意味着编译器不会在调用时验证参数。您有责任确保您使用一个 int 类型的参数调用 foo


6.7.6.3 Function declarators (including prototypes)

This part of the standard deals with 'Identifier list' and 'Parameter type list'.

First of all, function declaration (not definition) is same as the function prototype. Am I correct? If this is right, then why the standard says 'including prototypes'?

I am unable to understand the difference between 'Identifier list' and 'Parameter type list' with respect to function declaration.

int fun();    // Declaration
int fun(int x)// Definition, but the signature doesn't match and it works.
{ return x; }

Can someone explain, I am confused?

解决方案

Function declaration is not the same as function prototype. Prototype is a special kind of declaration. For example, this is a function declaration that is not a prototype

int foo();

And the following declarations are prototypes

int foo(int a, double b);
int bar(char, float);
float baz(void);

I.e. prototype is a declaration that describes the number and the types of function parameters. A non-prototype declaration does not say anything about parameters.

Now, C language still supports old K&R-style function definitions in addition to "modern" function definitions. K&R-style function definition looks as follows

int foo(a, b)
int a;
double b;
{
  ...
}

A "modern" function definition looks as follows

int foo(int a, double b)
{
  ...
}

As you can see, the K&R-style parameter list is just a, b. It includes parameter names, but does not include their types. This is what the grammar refers to as identifier-list. The "modern" parameter list is int a, double b and this is what the grammar refers to as parameter-type-list.

I.e. identifier-list is a part of K&R-style function definition syntax, while parameter-type-list is a part of "modern" function definition syntax.

Note also that in C language the declaration

int foo();

does not mean that foo takes no arguments. It means that foo take an unspecified number of arguments, i.e. it simply turns off argument type checking, argument number checking and argument conversions for foo at the point of the call. Such "signature" will match a definition for foo with any parameter list. This

int foo(int x)
{
  ...
}

is a perfectly valid definition for foo declared as shown above. The fact that it is declared with () simply means that the compiler will not verify the arguments at the point of the call. It will be your responsibility to make sure that you are calling foo with exactly one argument of int type.

这篇关于标识符列表Vs C中的参数类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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