Local and External variables

Local variables are variables that come into existence only when the function is called and disappears thereafter whereas external variables are accessible to all functions of the program.
To illustrate the difference between these two kinds of variables ,consider a function ‘getline()’
At first we consider the case where local variables are used. The call and the function definition in this case would be as follows:
Function call:
Function definition:
int getline(char s[],int lim)
Here there is a requirement to pass the arguments from the caller function to the called function through the parenthesis as the variables used are local to the function. The value of the variable ‘line’ is internal to the caller function.
Whereas when we consider the case of external variables ,the situation changes. Consider the case of declaring variables externally:
#define MAXLINE 100
char line[MAXLINE];
/*external variable declaration/*

The variables line has been declared externally.
Hence the call and definition of the function ‘getline()’ could be modified as follows:
Function call:
Function definition:
int getline(void)

The modified values of the variables would be available to all the functions.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s