[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

C:scope,visibility & extent of a variable

     I'm confused about the concepts of scope,visibility and extent of a variable in C.I'M NOT ABLE TO DISTINGUISH ONE
CONCEPT FROM THE OTHER.Please help me.How are they
related with each other ?
0
stsatish
Asked:
stsatish
  • 3
  • 3
1 Solution
 
stsatishAuthor Commented:
I'm a beginner in C, I've looked up several books and none
of them have been able to help me out as far as my problem
is concerned.I hope you do.Thankyou.
0
 
mjswartCommented:
Scope: means the lines of the code where a variable is valid. So a global variable's scope would be a whole file while a functions variable would be only the lines of a function.
Extent: means the same as scope.
Visibility: refers to whether a variable is global or local.

The following demonstrates a local and a global variable. If this doesn't help as far as your problem is concerned, could you give more detail on your problem?

/* global variables */
int a;  /* this variable may be referenced
             anywhere in this file */

/* prototype for a function */
int otherfunction();

int main()
{
    /* local variables */
    int b=1; /* this variable may only be
                    referenced in this function */
    a = b;
    otherfunction();
}

int otherfunction()
{
    printf("%d", a); /* valid */
    printf("%d", b); /* invalid */
}
0
 
stsatishAuthor Commented:
Thankyou for your help.Regarding the visibility of a variable:
                Can a local variable become invisible inside the function where it is declared?
0
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
mjswartCommented:
Not at all,  but it can be overridden:

int myfunction()
{
    int i = 0;
   
   /* start of block (you can do this to limit scopes)*/
   {
       float i = 4.5;
       /* within this block, i is a long */
    }
    /* end of block, i is an int again. */

}


hope it h;elps
0
 
stsatishAuthor Commented:
Thank you.I'm baffled by the following
declaration:

  int (*x) [ ];

 Can you please clarify?
0
 
mjswartCommented:
sorry, It looks like a pointer to a function and I've always stayed clear of those..

Sorry I couldn't help
0

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now