Declarations

A declaration introduces one or several names to a program – it informs the compiler what the name represents, what its type is, what operations are allowed with it, etc. This section reviews concepts related to declarations: declarations, definitions, declaration specifiers, and initialization.

The range of objects that can be declared includes:

Declarations and Definitions

Defining declarations, also known as definitions, beside introducing the name of an object, also establish the creation (where and when) of an object; that is, the allocation of physical memory and its possible initialization. Referencing declarations, or just declarations, simply make their identifiers and types known to the compiler.

Here is an overview. Declaration is also a definition, except if:

There can be many referencing declarations for the same identifier, especially in a multifile program, but only one defining declaration for that identifier is allowed.

For example:

/* Here is a nondefining declaration of function max; */
/* it merely informs compiler that max is a function */
int max();

/* Here is a definition of function max: */
int max(int x, int y) {
  return (x >= y) ? x : y;
}

/* Definition of variable i: */
int i;

/* Following line is an error, i is already defined! */
int i;

Declarations and Declarators

The declaration contains specifier(s) followed by one or more identifiers (declarators). The declaration begins with optional storage class specifiers, type specifiers, and other modifiers. The identifiers are separated by commas and the list is terminated by a semicolon.

Declarations of variable identifiers have the following pattern:

storage-class [type-qualifier] type var1 [=init1], var2 [=init2], ... ;

where var1, var2,... are any sequence of distinct identifiers with optional initializers. Each of the variables is declared to be of type; if omitted, type defaults to int. The specifier storage-class can take the values extern, static, register, or the default auto. Optional type-qualifier can take values const or volatile. For more details, refer to Storage Classes and Type Qualifiers.

For example:

/* Create 3 integer variables called x, y, and z
   and initialize x and y to the values 1 and 2, respectively: */
int x = 1, y = 2, z;   // z remains uninitialized

/* Create a floating-point variable q with static modifier,
   and initialize it to 0.25: */
static float q = .25;

These are all defining declarations; storage is allocated and any optional initializers are applied.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code