Lect 13P. 1 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Lect 13P. 1

Description:

Scope of Variables Lecture 13 Scope of Variables The – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 10
Provided by: RickFr6
Category:
Tags: 13p | lect | variable | what

less

Transcript and Presenter's Notes

Title: Lect 13P. 1


1
Scope of Variables
  • Lecture 13

2
Scope of Variables
  • The "scope" of a variable is the region within a
    program where that variable name is defined.
  • A variable that is declared before any statements
    which define a function (such as "void main ( )"
    ) has file scope. That is, its name, its
    designated memory location, and any value
    assigned to it will be known throughout the main
    function and any other function (subprogram) in
    the same file of source code.

3
Scope of Variables
  • A variable that is declared immediately after the
    statements " void main " will have the main
    program as its scope but will not be known
    outside the main function (for instance, not in
    function scanf). This is known as block scope.
  • A variable that is declared inside a block of
    statements (inside or after the "" ) will have
    only that block as its scope. It's scope ends at
    the "".
  • It is less confusing when all declarations either
    come before any functions (global variables) or
    only after the first "" in a function (local
    variables).

4
Scope of Variables
  • The same variable name can not be defined
    (declared) twice within the same block of code.
    A block of code is the code inside of a set of
    braces . However, a variable in C can be
    declared in other blocks of code (and even within
    nested blocks).
  • If the same variable name is declared in
    multiple nested blocks of code, the declaration
    that is within the particular block of code that
    is being executed at that time is the one that is
    currently "in scope" or in effect.

5
Scope of Variables Example
  • The following program will not compile because x
    is only defined inside the if block of statements
    and is not known outside that block (neither
    before nor after it).

6
Scope of Variables
  • / This program doesn't compile, x is not
    declared /
  • include ltstdio.hgt
  • int main ( )
  • FILE fptr
  • fptr fopen ("scope.dat", "w")
  • fprintf (fptr, before if block, xd\n", x)
  • if ( x 3 ) / x not declared yet --
    error /
  • float x 4.5
  • fprintf (fptr, "in if block, xf\n", x)
  • fprintf( fptr, "after if block, xd\n", x)

7
Scope of Variables
  • include ltstdio.hgt
  • int main ( )
  • int x 3 / Now "x" known, program
    compiles /
  • FILE fptr
  • fptr fopen ("scope.dat", "w")
  • fprintf (fptr, before if block, xd\n", x)
  • if ( x 3 )
  • float x 4.5
  • fprintf (fptr, "inside if block, xf\n",
    x)
  • fprintf (fptr, "after if block, xd\n", x)

8
Scope of Variables
  • When the program is modified as was shown on the
    previous slide, x is known throughout the
    program, but the x inside the if block of
    statements has a different data type and has a
    different value than the one outside the block.
  • These two versions of the variable, x, have two
    different memory locations, and thus we have two
    completely different variables with the same
    name. (Confusing, eh?)

9
Scope of Variables
  • As proof, let's look at the output file from the
    program, which was written to "scope.dat"
  • before if block, x3
  • inside if block, x4.500000
  • after if block, x3
  • Note that when the if block is in scope, x is a
    floating point variable whose value is 4.5, but
    outside the if block it is an integer variable
    with a value of 3.
Write a Comment
User Comments (0)
About PowerShow.com