What are Declarations?

Compiler DesignProgramming LanguagesComputer Programming

A declaration in a program refers to a statement that provides the data about the name and type of data objects to the programming language translators. For example, consider the following C declaration −

int a, b;

This declaration provides the programming language translator with the information that a and b are the data objects of type integer that are needed during the execution of the subprogram. The declaration also defines the binding of the data objects to the name a and b during their lifetimes.

Purposes of Declaration

The various purposes of declarations are as follows −

  • Type Checking − The declaration allows the programmers for static type checking i.e., checking the types of data objects at compile time rather than at execution time.

  • Choice of storage representation − The declaration supports the data about the type of the declared data object which helps the programming language translator to decide the good possible storage representation for that data object. This helps in reducing the overall storage requirement and execution time for the program being translated.

  • Storage Management − The declarations also serve to indicate the desired lifetime of the data object that creates it possible to use a more effective storage management process during program execution. For example, in C some of the data objects can be declared at the beginning of a subprogram while some other data objects are generated dynamically by the use of a specific function malloc.

  • Polymorphic Operations − An operation is said to be a polymorphic operation if an operation may take on a variety of implementation depending upon the types of its arguments. The declaration allows the programming language translator to decide at compile time the specific operation named by an overloaded operation symbol. For example in C, the declarations of data object a and b helps in determining the possible addition operation (integer addition or float addition) named by a + b.

Types of Declaration

There are two types of declarations which are as follows −

  • Explicit Declaration − An explicit declaration is a statement in a program that lists data object names and defines that they are a specific type. The explicit declaration creates a static binding to type. The example of an explicit declaration consists of the following C declaration.

int a, b;

  • Implicit Declaration − An implicit declaration is a declaration that holds when no explicit declarations are given. For example, in FORTRAN, an identifier that develops in a program that is not explicitly declared is implicitly declared as per the following convention: If the identifier starts with one of the letters I, J, K, L, M, or N it is implicitly declared to be of integer type; otherwise it is implicitly declared to be REAL type.

Published on 22-Oct-2021 11:46:40