Declarations and Definitions

  • storage class specifier: extern, static, auto, register, typedef
  • type specifier: void, char, int, unsigned,
  • type qualifier: const, restrict, volatile
  • All instances of a particular name with external linkage refer to the same object in the program
  • Unless prefixed it with extern, the declaration of a data object at the outer level is also a definition
  • A function prototype is a declaration of a function that declares the types of its parameters.

file1.c:

int x;  /* a declaration, and also a definition, external linkage */
static int y;  /* a declaration, and also a definition, internal linkage */
int z;  /* see declaration in file2.c */
void f1( void );  /* a declaration, and also a function prototype, defined in file2.c */

int main( void ) {
  x++;  /* x incremented from 0 to 1 */
  f1();  /* x incremented from 1 to 2 */
}

file2.c:

int x;   /* a declaration, and also a definition, external linkage, same variable as x in file1.c  */
static int y;  /* a declaration, and also a definition, internal linkage, totally separate from y in file1.c */
extern int z;  /* a declaration, defined in file1.c */

void f( void ) {
  x++;
}

Notes:

  • External Linkage: can be accessed from this file, and from other files as well
  • Internal Linkage: can be accessed only from current file