How are identifiers used in functions

6.4 Storage Classes and Scopes of Names

- So far we already have the 4 storage classes in different places external, automobile, static and register met briefly

- In the context of functions, now is a good time to deepen this knowledge

1. Storage class automobile

- All variables that are not explicitly assigned a storage class and that are not declared outside of functions fall into the storage class automobile

- You can explicitly define the storage class of a variable automobile assign by placing the keyword. in front of the type specification in the variable declaration automobile puts

- Automatic variables are created anew each time the function is called and are destroyed again when the function is exited

- therefore their scope is limited to the local function in which they were agreed

- This also applies to blocks (variables which are agreed within blocks and which are in the storage class automobile are only known locally in this block)

- automobile Variables can be initialized as desired (not just with constants)

- uninitialized automobile Variables have an undefined value (there is no omission value!)

- automobile-Vectors can Not initialized

2. Storage class external

- All objects that are declared outside of functions are in the external storage class

- the function names themselves are also in the external storage class

- external objects are known from their agreement to the end of the source code and even in other source code files

- The declaration of an object as external can be a definition or a declaration

- the following agreements (always outside of functions) differ significantly:

int sp = 0; / * Definition * / double vector137; extern int sp; / * Declaration * / extern double vector [];

- at a definition a variable is created

- An initialization is only possible here

- If there is no initialization, the omission value 0 is used

- at a declaration only the variable properties are specified, the variable itself must be defined elsewhere in the source code

- Declarations for a certain variable may appear several times in the (entire) source code, but a definition only once

Sample program p6-7.c

/ * * p6-7.c * Example program 7, section 6 * / #include #define BUFSIZE 100 char buff [BUFSIZE]; / * Buffer for ungetch () * / int bufp = 0; / * next free position * / / * (possibly set back) get characters * / int getch () {return ((bufp> 0)? buff [- bufp]: getchar ()); } / * getch () * / / * put characters back * / int ungetch (int c) {if (bufp> BUFSIZE) printf ("ungetch: too many characters \ n"); else buff [bufp ++] = c; } / * ungetch () * /

3. Storage class static

- static Objects can be both internal and external

- static Objects within functions are only known locally, unlike retained automobile Objects with their values ​​between function calls

- With regard to the initialization, the same applies as for external Objects, static vectors can therefore be initialized

- Strings within functions are always in the storage class static (e.g. printf () - parameter string)

- static Objects are outside of functions external Objects whose names are only known in this source code file

- Examples for the initialization of a vector:

static int ndigit [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; char string [] = "This is a string";

4. Storage class register

- This storage class can contain simple variables

- It corresponds in its other properties to the storage class automobile

- the compiler tries register To use variables in such a way that they are held in a real hardware register of the CPU

- if this is not possible, so will register ignored and the variable like automobile treated


6.5 Recursive functions

- Functions may not be nested, but they can call themselves directly or indirectly

- each time the function is called, new local variables are created (except for the storage class static)

- Many problems can be solved compactly and elegantly by means of recursion

Sample program p6-8.c

/ * * p6-8.c * Example program 8, section 6 * Output n decimal * Without recursion * / #include void printd (int n) {char s [10]; int i; / * Output sign * / if (n <0) {putchar ('-'); n = -n; } / * get the next character and remove it by division * / i = 0; do {s [i ++] = n% 10 + '0'; } while ((n / = 10)> 0); / * output in reverse order * / while (--i> = 0) putchar (s [i]); } / * printd () * /

Sample program p6-9.c

/ * * p6-9.c * sample program 9, section 6 * output n decimal * with recursion * / #include void printd (int n) {int i; / * Sign * / if (n <0) {putchar ('-'); n = -n; } / * call printd recursively * / if ((i = n / 10)! = 0) printd (i); putchar (n% 10 + '0'); } / * printd () * /

Sample program p6-10.c

/ * * p6-10.c * Example program 10, section 6 * Calculate the factorial * / long fak (int n) {if (n <= 1) return (1); else return (n * fak (n - 1)); } / * fak () * /

back to the table of contents