Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - Scope example

Node:Scope example, Next:, Previous:Communication via parameters, Up:Scope



Scope example

Notice that there are two variables named my_var in the example below, both visible in the same place. When two or more variables visible in one area of code have the same name, the last variable to be defined takes priority. (Technically adept readers will realize that this is because it was the last one onto the variable stack.)

/***************************************************************/
/*                                                             */
/* SCOPE                                                       */
/*                                                             */
/***************************************************************/

#include <stdio.h>

int main ()
{
  int my_var = 3;

  {
    int my_var = 5;
    printf ("my_var=%d\n", my_var);
  }

  printf ("my_var=%d\n", my_var);

  exit(0);
}

When you run this example, it will print out the following text:

my_var=5
my_var=3

 
 
  Published under free license. Design by Interspire