470,810 Members | 1,433 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

variable scope in C

6
Hi,

This is more theoretical question. When one talks about variable scope in C, there are two ways to explain it.
1) the four types of scopes: file, function, block, function-prototype
2) scope according to the storage class

Which one of these is the best way to explain scope in C? Function scope refers to the label identifier. Arent the local variables in function within function scope? What do you mean by function-prtottype? Any one example please.

Thanks.
Nov 1 '06 #1
4 6781
Banfa
9,065 Expert Mod 8TB
Variable scope and storage class are 2 different things.

Scope indicates where in the code you may use a variable

storage class indicates where in memory the variable is stored.

I would say there are 3 variable scopes, 4 if you include prototype-scope but that that doesn't seem useful to me
  1. Program Scope: A variable that is declared outside of all code blocks and is not declared static is accessable from any part of the program (although use of an extern statement will be required).
  2. File Scope: A variable that is declared outside of all code blocks and is declared static is accessable from any part of the file following the declaration.
  3. Local or Block Scope: A variable declared inside a code block is only accessable within that block. The parameters of a function are consider to have local scope as if they had been decared inside the block enclosing the function body.
  4. Prototype Scope: variables declared with-in a prototype have scope only up to the end of the prototype. meaning that for

    int Multiple(int x, int y);
    int Add(int x, int y);

    because x and y have prototype scope because they are out of scope at the end of Multiply you don't have a problem when you use them again in Add.

Function scope does exist but only applies to labels. They may be used anywhere inside the function but not externally to the function so

Expand|Select|Wrap|Line Numbers
  1. void fn()
  2. {
  3.     goto TheLabel;
  4.  
  5.     if (someCondition)
  6.     {
  7.  
  8. TheLabel:
  9.     }
  10. }
  11.  
works because TheLabel has function scope is accessable anywhere inside the function.

For C++ there is also Class Scope, variable declared in the class are accessable only within the class.


Storage Class can basically be extern, static, auto or register.

extern and static storage classes declare variables with global lifetime, the exist for the lifetime of the program.

auto and register declare variable of local lifetime, the only exist while the current code block executes.

The default storage type inside a code block is auto so you rarely see this keyword in code as it can be left out.

Register is also not common any more. It is a instruction to the compiler to dedicate a processor register to store the variable rather than using RAM. This would be used to optimise code for speed of execution. Since the advent of good optomising compilers it is generally considered better not to use this key word and to give the optomiser free reign to select whatever optomisations are best.
Nov 1 '06 #2
MyCGal
6
Variable scope and storage class are 2 different things.

Scope indicates where in the code you may use a variable

storage class indicates where in memory the variable is stored.

I would say there are 3 variable scopes, 4 if you include prototype-scope but that that doesn't seem useful to me
  1. Program Scope: A variable that is declared outside of all code blocks and is not declared static is accessable from any part of the program (although use of an extern statement will be required).
  2. File Scope: A variable that is declared outside of all code blocks and is declared static is accessable from any part of the file following the declaration.
  3. Local or Block Scope: A variable declared inside a code block is only accessable within that block. The parameters of a function are consider to have local scope as if they had been decared inside the block enclosing the function body.
  4. Prototype Scope: variables declared with-in a prototype have scope only up to the end of the prototype. meaning that for

    int Multiple(int x, int y);
    int Add(int x, int y);

    because x and y have prototype scope because they are out of scope at the end of Multiply you don't have a problem when you use them again in Add.

Function scope does exist but only applies to labels. They may be used anywhere inside the function but not externally to the function so

Expand|Select|Wrap|Line Numbers
  1. void fn()
  2. {
  3.     goto TheLabel;
  4.  
  5.     if (someCondition)
  6.     {
  7.  
  8. TheLabel:
  9.     }
  10. }
  11.  
works because TheLabel has function scope is accessable anywhere inside the function.

For C++ there is also Class Scope, variable declared in the class are accessable only within the class.


Storage Class can basically be extern, static, auto or register.

extern and static storage classes declare variables with global lifetime, the exist for the lifetime of the program.

auto and register declare variable of local lifetime, the only exist while the current code block executes.

The default storage type inside a code block is auto so you rarely see this keyword in code as it can be left out.

Register is also not common any more. It is a instruction to the compiler to dedicate a processor register to store the variable rather than using RAM. This would be used to optimise code for speed of execution. Since the advent of good optomising compilers it is generally considered better not to use this key word and to give the optomiser free reign to select whatever optomisations are best.

Thanks for the explaination.

I am a little bit confused about difference between program scope and file scope.
Program scope----
Expand|Select|Wrap|Line Numbers
  1. int i;
  2. main()
  3. {
  4.  increment();
  5.  increment();
  6. }
  7.  
  8. increment()
  9. {
  10. extern i;
  11. i=i+1;
  12. printf("i= %d\n",i);
  13. }
File scope----
Expand|Select|Wrap|Line Numbers
  1. static int i;
  2. main()
  3. {
  4. main()
  5. {
  6.  increment();
  7.  increment();
  8. }
  9.  
  10. increment()
  11. {
  12. i=i+1;
  13. printf("i= %d\n",i);
  14. }
For above two code samples, both the variables i are available anywhere in the entire code. What is the difference? Or what difference does static and extern keyword make?

I sincerely appreciate your help.
Nov 2 '06 #3
Banfa
9,065 Expert Mod 8TB
For above two code samples, both the variables i are available anywhere in the entire code.
But only because you have all your code in 1 file try this example consisting of 3 files

Expand|Select|Wrap|Line Numbers
  1. // Program.h - header for the entire program
  2.  
  3. #include <stdio.h>
  4.  
  5. extern int File2Variable;
  6.  
  7. extern void DisplayFile2();
  8.  
Expand|Select|Wrap|Line Numbers
  1. // File1.cpp
  2.  
  3. #include "Program.h"
  4.  
  5. static int File1Variable = 5;
  6.  
  7. int main()
  8. {
  9.     DisplayFile2();
  10.     printf("File1Variable = %d\n", File1Variable);
  11.     File2Variable = 50;
  12.     DisplayFile2();
  13. }
  14.  
Expand|Select|Wrap|Line Numbers
  1. // File2.cpp
  2.  
  3. #include "Program.h"
  4.  
  5. int File2Variable = 30;
  6.  
  7. void DisplayFile2()
  8. {
  9.     printf("File2Variable = %d\n", File2Variable);
  10. }
  11.  
File1Variable is declared static, this means that it is only accessable in File1.cpp, it has file scope. However File2Variable is not declared static, it has scope across the whole program and is accessable in any file. However to use it has to be declared, this is done in the header file using the extern keyword

extern int File2Variable;

This indicates to the compiler that somewhere in the program there will be a variable call File2Variable of type int so it does cause an error when it is seen in code. This statement doesn't define any data and you can not put an initialiser on it, this will cause a compiler error

extern int File2Variable = 5;

Without the

int File2Variable = 30;

in File2.cpp you will get a linker error because File2Variable will not be defined anywhere.



I suggest you play around a bit.

It is good practise to put your declarations into a header because then they can easily be included into multiple files and if they change you only have to change them in 1 place. It is also good practice to include the header into the file where the symbol is defined (I have included Program.h in File2.cpp) so that if you change either the definition or declaration you will get a compiler error if you don't also change the other one.
Nov 2 '06 #4
Banfa
9,065 Expert Mod 8TB
I final note, if you are doing a course of some sort you may need to learn the answers you professor is expecting to get, my view is a real world view not an acedemic one and they sometimes don't co-inside.
Nov 2 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Grant Wagner | last post: by
4 posts views Thread by Gery D. Dorazio | last post: by
23 posts views Thread by Russ Chinoy | last post: by
pbmods
1 post views Thread by pbmods | last post: by
2 posts views Thread by Shraddha | last post: by
112 posts views Thread by istillshine | last post: by
3 posts views Thread by SRoubtsov | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.