Friday, July 29, 2016

C Basics - Declaration in C

A declaration specifies the interpretation and attributes of a set of identifiers. A declaration that also causes storage to be reserved for the object or function named by the identifier is called a definition.

Declarations are made up of some combination of storage-class specifiers, type specifiers, type qualifiers, declarators, and initializers.

type-specifier gives the data type of the variable, declarator gives the name of the variable, storage-class specifiers gives linkage and storage duration i,e. how the declared item is stored and initialized, and which parts of a program can reference the item.

A declaration must have at least one declarator, or its type specifier must declare a structure tag, union tag, or members of an enumeration. Declarators provide any remaining information about an identifier. A declarator is an identifier that can be modified with brackets ([ ]), asterisks (*), or parentheses ( ( ) ) to declare an array, pointer, or function type, respectively.

All definitions are implicitly declarations, but not all declarations are definitions. Storage is not allocated by "referencing" declarations, nor can variables be initialized in declarations.

The location of the declaration within the source program and the presence or absence of other declarations of the variable are important factors in determining the lifetime of variables. There can be multiple redeclarations but only one definition. However, a definition can appear in more than one translation unit.

Declarations outside all function definitions are said to appear at the "external level." Declarations within function definitions appear at the "internal level."

External variables are variables at file scope. They are defined outside any function, and they are potentially available to many functions. In variable declarations at the external level you can use the static or extern storage-class specifier.

You can use any of four storage-class-specifier auto, register, static or extern terminals for variable declarations at the internal level.



Related topics:
Tokens in C   |   Character Set in C   |   White Space Characters in C   |   Trigraph Characters in C   |   Extended Characters in C   |   Escape Sequence in C   |   Comments in C   |   Keywords in C   |   Identifiers in C

List of topics: C Programming

No comments:

Post a Comment