Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifier list Vs Parameter type list in C

Tags:

c

function

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?

like image 362
StackIT Avatar asked Sep 16 '13 05:09

StackIT


3 Answers

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.

like image 94
AnT Avatar answered Oct 15 '22 02:10

AnT


In C11 standard

6.7.6.3 Function declarators (including prototypes)
Constraints

D( parameter-type-list )
or
D( identifier-listopt )  

While declaring function you need not give identifiers list. BUt you should at least mention type list

example:

int sum(int,int); //declaration  

int sum(int a,int b); //declaration

both are declaration of same function .

but second one you also mentioned identifiers that is optional.

like image 35
Gangadhar Avatar answered Oct 15 '22 02:10

Gangadhar


C11(ISO/IEC 9899:201x) §6.2.1 Scopes of identifiers Section 2

A function prototype is a declaration of a function that declares the types of its parameters.

As of your example, for the function definition

int fun(int x)
{ return x; }

Both int func(int x); and int func(); are valid function declarations. But only the former is a function prototype.

You can even omit the variable names in function declarations, e.g, int func(int);. Although we usually prefer not to do it because of lack of readability.

like image 33
Yu Hao Avatar answered Oct 15 '22 00:10

Yu Hao