Declaration and difference between prototype

What is the difference between a declaration and a prototype in C? In what situations are they called declarations and in what prototypes?

+4
source share
4 answers

TL DR; . All prototypes are declarations, but not all declarations are prototypes.

A declaration is a general terminology used in standards; a prototype is more specific.

Quote C11, chapter §6.7

An ad defines the interpretation and attributes of a set of identifiers. [...]

and from §6.7.6,

, , , , , , .

, §6.2.1

[....] prototype - , .

, , - ( ) .


"": §6.4.2.1,

( _, ) , , 6.2.1. [...]

§6.2.1,

; ; , ; typedef; ; ; . [....]

+9

:

int a;    // "a" has type int

( ) :

int f();   // a function call expression "f(x, y, ...)" has type int

, ; , . ( ):

int g1(void);         // "g1" takes no arguments, "g()" has type int
int g2(float, char);  // "g2" takes two arguments
int g3(int, ...);     // "g3" takes at least one argument

, : , . , , ( , - ).

, , "" : int g2(); int g2(float, char) { return 0; }, g2 , - . , .

, , , (...). <stdarg.h> , -.

+6
  • - , ;.

  • - , .

, : void func (void);
: - : void func ();.

- (6.11.6), C. .

+4

C (6.2.1 )

  1. ... ( - , .)

.

, ,

void f();

void f( void );

The first declaration is not a prototype, because nothing is known about the parameters of the function.

The second declaration is a prototype because it provides a list of function parameter types (this is a special type of type list that indicates that the function has no parameters).

0
source

Source: https://habr.com/ru/post/1676353/


All Articles