The Three Attributes of an Identifier Identifiers have three essential attributes: storage duration scope linkage CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Storage Duration storage duration determines when memory is set aside for the variable and when that memory is released automatic allocated when the surrounding block is executed deallocated when the block terminates static stays in the same storage location as long as the program is running can retain its value indefinitely (until program terminates) CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Scope scope (of an identifier) the range of program statements within which the identifier is recognized as a valid name block scope visible from its point of declaration to the end of the enclosing block place declaration within a block file scope visible from its point of declaration to the end of the enclosing file place declaration outside of all blocks (typically at beginning of file) CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Linkage linkage determines the extent to which the variable can be shared by different parts of the program external linkage may be shared by several (or all) files in the program internal linkage restricted to a single file, but shared by all functions within that file no linkage restricted to a single function CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Determining the Attributes The default storage duration, scope and linkage of a variable depend on the location of its declaration: inside a block automatic storage duration, block scope, no linkage outside any block static storage duration, file scope, external linkage When the defaults are not satisfactory, see: auto static extern register CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Function Declaration vs Definition A function name is an identifier, so it must be formally declared before it is used. Unlike a simple variable or constant, a function has a return type and (possibly) a formal parameter list. The function declaration must also specify those. The function declaration is essentially just a copy of the function header from the function definition. A function declaration must specify the types of the formal parameters, but formal parameter names are optional. However, it is good practice to include the formal parameter names in the function declaration. double circleArea(double Radius); double circleArea(double Radius) { const double PI = 3.141596224; return (PI * Radius * Radius); } Many authors refer to a function declaration as a prototype. Function declarations are typically declared at file scope or within a header file, although they may be placed anywhere. The placement determines the scope of the function name, and hence where it may be called. CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Formal Parameters are Pass-by-Value Formal parameters have automatic storage duration and block scope, just like local variables. Formal parameters are automatically initialized with a copy of the value of the corresponding actual parameter. There is no connection between the actual and formal parameters other than that they store the same value at the time of the function call. In Java, you can pass a function a reference to an object (in fact, there's no other way to pass an object) and the function can use the reference to modify the object that's held by the calling code. In C, you can't do that until you understand how pointers work… CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Typical C Code Organization For now, we'll restrict our attention to single-file programs. The typical organization is: // include directives . . . // file-scoped declarations of functions // and constants int main() { } // implementations of other functions CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Example Program #include <stdio.h> #include <math.h> #include <stdbool.h> int nextPrimeAfter(int Start); bool isPrime(int Value); int main() { int Value; printf("Enter a positive integer: "); fflush(stdout); scanf("%d", &Value); printf("\n"); while ( Value <= 0 ) { printf("No, I said a POSITIVE integer: "); } // continues. . . CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Example Program // . . . continued int Prime = 2; while ( Value > 1 ) { bool factorFound = false; while ( Value % Prime == 0 ) { if ( !factorFound ) printf("Prime factor: "); printf("%5d", Prime); factorFound = true; Value = Value / Prime; } if ( factorFound ) printf("\n"); Prime = nextPrimeAfter( Prime ); return 0; CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Example Program int nextPrimeAfter(int Start) { int Value = Start + 1; while ( !isPrime( Value ) ) { ++Value; } return Value; CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens
Example Program bool isPrime(int Value) { int Ceiling = (int) sqrt( Value ); for (int Divisor = 2; Divisor <= Ceiling; Divisor++) { if ( Value % Divisor == 0 ) return false; } return true; CS@VT August 2009 ©2006-09 McQuain, Feng & Ribbens