Tutorial 7.2: Variable Scope

Variable Scope
All examples that have been presented in the previous tutorials declared variables in the block of function main(). These variables are known only within the block of function main() which means, the variables are not accessible outside of the block. This is also applied to variables which are defined within a block that is inside other block. But variables which are defined in the outer block are accessible in the inner block. This is illustrated in the following example.
#include <stdio.h>

int main()
{
 int a = 0;
 printf("a\tb\n");
 
 while(++a < 10) {
  int b = 1;
  printf("%d\t%d\n", a, b);
  ++b;
 }
 
 return 0;
}
In the example there are two variables declared,. Variable a is declared within function main() block and variable b is declared within while loop block. a is the outer block variable therefore it is accessible within inner block. But b is not accessible in the outer block because it is while loop variable. Thus if you try to print the value of b outside the while loop block, you will get an error message when you compile the program.

Variables declared outside any function is called global variables. A global variable is a variable that can be accessed in any function. Global variables are declared in the normal way except that (normally) they are declared before function main(). For example, variable c is a global variable since it is declared outside (before) function main().
#include <stdio.h>

int c = 10; // global variable

int main()
{
 ...
 
 return 0;
}

Next: Defining a Function

16 comments:

Unknown said...

I really enjoyed the article, this is very well-written and helpful. Although, I have been searching for long about the topic but finally I got the right article. The information you have shared with is really incredible. Keep it up
Please also send me a link for an informative blog on web design services sydney

Unknown said...
This comment has been removed by the author.
Unknown said...

The article is great and the site too. But the only problem is it does not contain programs like I saw it in c program download. I think you should add few programs it would make your site a great place for beginners.

kaurn said...

Very Useful Post!Thanks!
C Programming

krishna Choudhary said...

Very useful blog for beginners
Visit: C programming Course

Unknown said...

very nice..post.
https://shayarihindishayari.com/

Ramesh said...

Great Content. It will useful for knowledge seekers. Keep sharing your knowledge through this kind of article.
Node JS Training in Chennai
Perl Training in Chennai

shreekavi said...

Nice blog! Thanks for sharing this valuable information
Angularjs Training in Bangalore
Angularjs classes in pune
Angularjs Training in hyderabad
Angularjs Training in Gurgaon
Angularjs Training in delhi

rithi said...

This post is so interactive and informative.keep updating more information...
Artificial Intelligence Course in Mumbai
Artificial Intelligence Course in Ahmedabad
Artificial Intelligence Course in Kochi
Artificial Intelligence Course in Trivandrum
Artificial Intelligence Course in Kolkata

Reshma said...


Get inspired by your blog. Keep doing like this....
Spoken English Classes in Bangalore
Spoken English Classes in Pune
spoken english centre in Hyderabad
English Speaking Course in Gurgaon
English speaking course in Delhi

Akash said...

This blog helps me to get valuable information thanks for this informative blog.
oracle training in Chennai
oracle online course
oracle training in Coimbatore

krishna said...

This post is so helpfull and informative.keep updating with more information...
Hacker Interface
Hacking Technology

Monisha said...

Most useful information!!! Thanks for it.
Python Course in Chennai
Learn python online
Python Course in Coimbatore

Niyaz said...

Wonderful Blog post!!! I am more impressed with your data.
Why Pointers are not Used in Java
Why Java Doesn't Support Pointers

Deva said...


Thank you, This was the best and most interesting course. please continue to share your thoughts.

RPA Training in Chennai
RPA Training Online
RPA Training In Bangalore

Silviya26 said...


Nice post Thank for sharing

DevOps Online Training
DevOps Training in Chennai
DevOps Training in Bangalore

Post a Comment