Class 4: C Storage Class, Scope Linkage Flashcards
Storage Class
Determines the type of storage and lifetime of storage associated with a variable and the variables initialization if any
Storage scope
Determines which region of a program in which a variable can be accessed (by name and not through pointer)
Linkage
Determines whether the same name in a different file frees to the same variables or function or some different variable function
Stack
- A place in memory where information specific to particular function is stored each time it is invoked
- It is allocated when the function is called a deal located when he function returns
Heap
- A place in memory where information specific to a particular process is stored each time it is invoked
- Any valuesstored here are available until the process stops executing
Storage class
Refers to the type of memory in which a variable values is stored which defines different characteristics for variable
Static (keyword)
The heap
Automatic (keyword)
The runtime stack
Register (keyword)
Hardware registers
Do function identifiers have a storage class?
NO, they are not stored on the stack or the heap
Storage Class: Static
Declared outside all blocks.
Retain the last value they were assigned until the program completes
What value are storage class static variables automatically assigned to?
0
Storage Class: Automatic
Declared inside a block.
When are automatic variables created
Just before the execution of the code
When are automatic variables discarded
When they are not longer accessible/ just as execution leaves the block
What storage class do parameters of functions have????
AUTOMATIC
Variable declared inside a block but with the keyword static
This changes storage class from automatic (the default) to static. Although this changes show long the variable exists during the program, IT DOES NOT CHANGE THE SCOPE STILL BLOCK SCOPE
When keyword static is used within a block, it changes a variables ______
Storage class
When the keyword static is used outside of any/all blocks, it changes an identifiers ________
Linkage
Register variables
Stored in machines hardware registers rather than memory. Can be accessed faster. Created and destroyed at the same time as automatic variables
Scope
Refers to the point in a program in which an identifier may be used to access a variable directly (not through pointer)
Block scope
Variable can only be accessed within that block
File scope
Declared outside fo all blocks. The identifier can be accessed anywhere from the end of its declaration to end of the source file in which it was declared
Prototype scope
Only applies to parameter names DOES not apply to function names
Any variables with prototype scope has no _____ or ______
Memory class, linkage
This is because no memory has been allocated for the variable
Void function2(int var3, int var4);
Function2 : file scope
Var3: prototype scope
Var4: prototype scope
Void function1(int var1, int var2){}
Function1: file scope
Var1: block scope
Var2: block scope
Linkage
Determines how many occurrences of a file scope identifier with the same name (in different files we plan to use together) are treated
None linkage
Identifiers that have no linkage (variables without file scope). All block scope variables (NONE) and prototype scope variables (undefined)
Internal linkage
All references to the file scope identifier within ONE source file
External Linkage
All references to a file scope identifier in any source file refer to the entity
What is default linkage of file scope variable?
External
How can we change linkage of file scope variable from external to internal
Using the keyword static. This means that no programs except the .c file where the file scope variable is defined can access the variable
Using a variable with external linkage in another .c file
If you want to use a variable with external linkage in another .c file, you DECLARE it using external. You cannot omit this declaration and use the variable directly.