Friday, July 29, 2016

C Basics - Local and Global Variables in C

Variables can be declared in,
  • Inside a function or a block which is called local variables.
  • Outside of all functions which is called global variables.
  • In the definition of function parameters which are called formal parameters.
Scope of a variable is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed.

Local Variables:
Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code.

#include <stdio.h>
 
int main () {

  /* local variable declaration */
  int x, y;
  int z;
 
  /* variable initialization */
  x = 10;
  y = 20;
  z = x + y;
 
  printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
 
  return 0;
}

Global Variables:
Global variables are defined outside a function. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.

#include <stdio.h>
 
/* global variable declaration */
int z;
 
int main () {

  /* local variable declaration */
  int x, y;
 
  /* variable initialization */
  x = 10;
  y = 20;
  z = x + y;
 
  printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
 
  return 0;
}

A program can have same name for local and global variables but the value of local variable inside a function will take preference.

#include <stdio.h>
 
/* global variable declaration */
int x = 20;
 
int main () {

  /* local variable declaration */
  int x = 10;
 
  printf ("value of x = %d\n",  x);
 
  return 0;
}

The above program would output the following.

value of x = 10

Formal Parameters:
Formal parameters, are treated as local variables with-in a function and they take precedence over global variables.

#include <stdio.h>
 
/* global variable declaration */
int x = 20;
 
int main () {

  /* local variable declaration */
  int x = 10;
  int y = 20;
  int z = 0;

  printf ("value of x in main() = %d\n",  x);
  z = sum( x, y);
  printf ("value of z in main() = %d\n",  z);

  return 0;
}

/* function to add two integers */
int sum(int x, int y) {

   printf ("value of x in sum() = %d\n",  x);
   printf ("value of y in sum() = %d\n",  y);

   return x + y;
}
The above program would output the following.

value of x in main() = 10
value of x in sum() = 10
value of y in sum() = 20
value of z in main() = 30

Initializing Local and Global Variables:
When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them.
Data Type Initial Default Value
int 0
char '\0'
float 0.0
double 0.0
pointer NULL



Related topics:
Data Types - Type Specifiers in C   |   Const and Volatile - Type Qualifiers in C   |   Variables in C   |   Constants in C   |   Initialization in C

List of topics: C Programming

No comments:

Post a Comment