Saturday, 5 October 2013

What is Declaration and Definition in C


Important Terms – Declaration & Definition:

 

Declaration: Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function.

 

e.g.

Variable Declaration:                                                     Function Declaration:

int sum;                                                                                int GetSum(int a, int b);

char name[10];                                                                 void PrintArea (int height, int width);

float total;                                                                           float GetTotal();

 

 

 

Definition: When we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration. (or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function).

 

Note: Definition of a C function means writing the body of the function.

 

e.g.

Variable Definition:                                                       Function Definition:

int sum=0;                                                                           int GetSum(int a, int b) {…..}

char name[10]={‘M’,’a’,’h’,’e’,’s’,’h’};                    void PrintArea (int height, int width=10) {……}

float total=0.0f;                                                                 float GetTotal() {……}

 

Declaration of Variables

 

Declaration does two things:

  1. It tells the compiler what the variable name is.  
  2. It specifies what type of data the variable will hold.

 

The declaration of variable must be done before they are used in the program.

 

                There are two types of variable declaration:

 

1.       Primary type declaration - A variable can be used to store a value of any data type. the syntax for declaring a variable is as follows:

data type var1, var2, … , varn;

Where var1, var2, … , varn are the variable names.

                               

int count;

float sum, amount;

 

2.       User-defined type declaration – We declare the variables with keywords typedef, enum, union.

 

Declaring a Variable as ConstantWe may like the value of certain variables to remain constant during the execution of a program. We can achieve this by declaring the variable with the qualifier const at the time of initialization.

 

Ex:          const int  class_size = 40;

 

Const is a new data type qualifier defined by ANSI standard. This tells the compiler that the value of the int variable class_size must not be modified by the program. However, it can be used on the right_hand side of an assignment statement like any other variable.

 

Declaration of Variable as VolatileANSI standard defines another qualifier volatile that could be used to tell explicitly the compiler that a variable’s value may be changed at any time by some external source (from outside the program)  

 

For ex:   volatile int date;

 

The value of date may be altered by some external factors even if it does not appear on the left_hand side of an assignment statement. When we declare a variable as volatile, the compiler will examine the value of the variable each time it is encountered to see whether any external alteration has changed the value.

 

Note: the value of a variable declared as volatile can be modified by its own program as well. If we wish that the value must not be modified by its program while it may be alter by some other process, then we may declare the variable as both const and volatile as:    

 

volatile const int   location = 100;

 

Assigning values to variablesValue can be assign to variable using the assignment operator ‘=’ as follows:

Variable name = Constant or Expression

 

Ex.

balance = 5000;

year = 1999;

ans = ‘Y’;

y = a + b + 5;

y = y + 1;

 

·         C permits multiple assignments in one line as:

initial_value=0, final_value=100;

·         We can also assign a value to the variable at it’s declaration time as :

int initial_value = 0;

char yes = ‘Y’;

·         The process giving initial value to variable is called initialization. C permits the initialization of more than one variables in one statement using multiple assignment operators as :

p = q = r = 0;

x = y = z = MAX;

No comments:

Post a Comment