Identifiers - PowerPoint PPT Presentation

About This Presentation
Title:

Identifiers

Description:

Do identifier names need to unique? depends on scope of identifier, and ... const int LastN = n; /* dynamic ... { final int LastN = n; 9. Types of Constants ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 10
Provided by: kenneth67
Category:

less

Transcript and Presenter's Notes

Title: Identifiers


1
Identifiers Lifetimes
  • Named entities and their binding to storage.

2
Use of Identifiers
  • What parts of a program have identifiers
    ("names")?
  • variables
  • constants
  • subprograms
  • labels
  • formal parameters
  • classes
  • whole programs?

const int BUFSIZE 1024 static long total long
count( int value ) sum while(valuegt0)
total value-- return total
3
Naming of Identifiers
  • Do identifier names need to unique?
  • depends on scope of identifier, and
  • syntax of language use must be unambiguous
  • Can an identifier use the name of a keyword?
  • yes, in some (older) languages
  • in current language design, keywords are reserved
    for clarity

( In Pascal, identifier can be a keyword
) var integer real real integer begin real
2 3 ( integer arithmetic )
4
What kind of identifier is "A"?
(Java) Is the meaning of each identifier
unambiguous?
  • class A / Stop waste! Conserve letters. /
  • A A
  • A ( )
  • A (A A) this.A A
  • A A ( ) return A
  • A A (A A)
  • A if ( A.A( ) A ) return A
  • else break A
  • return this.A
  • public static void main(String a)
  • A A new A( )
  • A.A new A( A.A( A.A( ) ) )

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
5
Lifetime
  • Lifetime is the duration of time that an entity
    (variable, constant, ...) is bound to memory.
  • Lifetime can be...
  • duration of process execution (static variables)
  • duration of object existence (object attributes)
  • duration of function activation (local variables)
  • duration of scope activation (variable defined in
    a scope). A function also defines a scope.
  • Lifetime applies to entities that have values,
    not names.
  • "Lifetime of an identifier" doesn't make sense...
    identifiers have "scope" rather than "lifetime".

6
Lifetime and Scope
  • Scope of identifiers and lifetime of entity it
    refers to are not the same.
  • Lifetime time when entity exists in memory
  • scope the parts of a program where a name is
    known or "visible"

void add( int count ) static long SUM
0 float x 0.5 while(countgt0) int x
2 SUM xcount count-- printf("f",
x)
count scope is function add( ), lifetime
unknown. SUM scope is function body, lifetime is
process execution time. float x scope is part of
the function excluding while loop, lifetime is
function activation time. int x scope is while
loop, lifetime is duration of while loop.
7
Lifetime and Scope (2)
  • BUFSIZE has global scope (any file in this
    program can refer to its value). Its lifetime is
    the process's lifetime.
  • buf has file scope. Its lifetime is the
    program's lifetime.
  • length , k scope is the function, lifetime is
    duration of function.
  • c scope is the for loop. Life is duration of
    "for" loop execution.

int BUFSIZE 1024 static char
bufBUFSIZE void read( int length ) int
k for(k0 kltlength k) int c
getchar() if (clt0) break bufk
c bufk '\0'
8
Constants
  • C "const" can be compile time, load time, or run
    time constants
  • const int MaxSize 80 / compile time
    /
  • void mysub( const int n )
  • static const NUMBER 8sizeof(int)
  • const time_t now time(0) / dynamic /
  • const int LastN n / dynamic /
  • In Java, "final" merely means a variable cannot
    be changed after the first assignment.
  • final InputStream in System.in
  • void mysub ( int n )
  • final int LastN n

9
Types of Constants
  • There are several classes of constants, depending
    on when their value is known
  • compile time value can be computed at compile
    time, includes
  • manifest constants (literals) const int MAX
    1024
  • computable expressions const int MAXBYTES
    8MAX
  • elaboration time value is determined when the
    program is executed.
  • sometimes this simply means a variable whose
    value cannot be changed (this is enforced by the
    compiler).
Write a Comment
User Comments (0)
About PowerShow.com