C4: C Variables and types Flashcards
What is a variable?
It is an abstraction of memory cells. It has an identifier, a value, a type …(at least)
What is data type?
data type describes a variable, which value, how represented in memory, which operators are possible. There are two types: basic (e.g. integers) and user-defined (structs)
What is the difference between variable definition and declaration?
Variable declarations can be duplicated, have static name type binding done at compile time and specifiy identifier and type. Variable definition: only one allowed, assigns fixed address to variable (at runtime?). Definition and declaration of VARIABLE looks the same, possible: int a; but not so for functions.
What is initialization of a variable?
Initialization: assigns initial value to a variable, value not needed at compile time. Assigns scope-
What is scope?
Scope is the portion of program where name of variable (identifier) can be used to refer to a variable.
What is implicit dynamic memory management?
Auto variables (Storage class Auto) in memory. Memory is automatically allocated when program enters a block and deallocated when program leaves block.
What are the four kinds of scope_
- File Scope (identifiers declared outside blocks and function prototypes)
2 Block scope (used for identifiers declared inside a block and for function parameters = local variables) - Function scope (only used for label names (case:) not for variables
- function prototype scope (unsure what this is)
Why do we need storage classes and what are they?
Storage classes determine a variable’s lifetime, when and where memory is allocated/deallocated. The classes are
stored in stack:
1. Auto - default for block scope
2. Register - useful for often-used items in block scope
stored in bss or data segment:
3. Static - keeps value between function calls, has internal linkage
no memory assigned:
4. Extern - variable declared with extern keyword, has no definition. it is defined in another translation unit., A defined extern variable must have file scope. Undefined - it is not allocated memory.
Memory assigned to thread:
5. _Thread_local - each Thread gets a copy. Deallocated memory when Thread dies.
What is internal linkage?
Internal linkage (storage class static) is created when explictly writing “static” in front of a variable (see static storage class). The variable is not exported by the compiler to the inker. (It’s like a private variable.)
What is external linkage?
External linkage (in storage class extern) is create when the extern keyword is used to declare a variable. It means the variable’s definition is in another translation unit. No memory is assigned, compiler simply exports identifier.
What is ‘const’?
‘const’ is a keyword. constant int a = b for example. Declares, defines and initiates a constant variable. These constants must be initialized at definition time and cannot be modified later.
const int b; would create an error becasue b must be initialized at definition time.
What is ‘volatile’?
It is a keyword in declaration of a variable that is a hint to the compiler that the value of the variable may change at any time.
What is variable alignment?
it is a restriction on the starting location of a variables memory- The first memory position must start at the beginning of a particular block.
For exmample: alignas(4) int b; means that b can start at position 0, 4, 8, 12, …. or multiples of 4.
What are the basic data types in C?
Integer floating point characters Booleans Complex Numbers Enumerations Pointer
What is a pointer?
It is a basic data type that contains the address of a variable that has specific value