C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential attributes: storage duration scope linkage
C Part 2 Computer Organization I 2 August 2009 © 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)
C Part 2 Computer Organization I 3 August 2009 © 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)
C Part 2 Computer Organization I 4 August 2009 © 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
C Part 2 Computer Organization I 5 August 2009 © McQuain, Feng & Ribbens Determining the Attributes When the defaults are not satisfactory, see: auto static extern register 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
C Part 2 Computer Organization I 6 August 2009 © McQuain, Feng & Ribbens Function Declaration vs Definition double circleArea(double Radius) { const double PI = ; return (PI * Radius * Radius); } 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. double circleArea(double Radius); 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. Many authors refer to a function declaration as a prototype. 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.
C Part 2 Computer Organization I 7 August 2009 © 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…
C Part 2 Computer Organization I 8 August 2009 © McQuain, Feng & Ribbens Typical C Code Organization // include directives... // file-scoped declarations of functions // and constants... int main() {... } // implementations of other functions For now, we'll restrict our attention to single-file programs. The typical organization is:
C Part 2 Computer Organization I 9 August 2009 © McQuain, Feng & Ribbens Example Program #include 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: "); scanf("%d", &Value); printf("\n"); } // continues...
C Part 2 Computer Organization I 10 August 2009 © 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; }
C Part 2 Computer Organization I 11 August 2009 © McQuain, Feng & Ribbens Example Program int nextPrimeAfter(int Start) { int Value = Start + 1; while ( !isPrime( Value ) ) { ++Value; } return Value; }
C Part 2 Computer Organization I 12 August 2009 © 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; }