Saturday, November 8, 2008

Declaration and Definitions

Declaration and Definitions


Declaration:

A declaration tells the compiler about the nature of the variable. It is a hint to the compiler. No storage is assigned in a declaration. A declaration is a source code construct that associate the attribute with names.

An attribute can be a type, scope, storage duration or linkage.

Definition:

A definition is a place where the compiler assigns storage in addition to stating the nature of the variable. It also provided the information, the compiler needs to create code for that code.

Important points:

1. A function declaration is a definition if it contains function block.

2. Declarations that include initialize are always declarations.

3. All declarations with in the function block are definitions, unless they contains storage class specifier extern.

4. If you declare an object outside all functions, without an initialize, and without storage class specifier ‘extern’ the declaration is a tentative definition.

5. A tentative definition of a identifier remains a simple declaration if the translation unit contains another definition for the same identifier. Otherwise the compiler behaves as, if the tentative definition had included an initializer with the value zero, making it a definition.

Example:

int a = 12; /* Definition*/

extern double b[]; /*Declaration */

int i, v[]; /*Tentative Definition */

static int j; /*Tentative Deifinition */

void func()

{

extern char c; /*Declaration */

static short d; /*Definition*/

float e; /*Definition*/

}

No comments: