C4: C Variables and types Flashcards

1
Q

What is a variable?

A

It is an abstraction of memory cells. It has an identifier, a value, a type …(at least)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is data type?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between variable definition and declaration?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is initialization of a variable?

A

Initialization: assigns initial value to a variable, value not needed at compile time. Assigns scope-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is scope?

A

Scope is the portion of program where name of variable (identifier) can be used to refer to a variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is implicit dynamic memory management?

A

Auto variables (Storage class Auto) in memory. Memory is automatically allocated when program enters a block and deallocated when program leaves block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the four kinds of scope_

A
  1. 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)
  2. Function scope (only used for label names (case:) not for variables
  3. function prototype scope (unsure what this is)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why do we need storage classes and what are they?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is internal linkage?

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is external linkage?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is ‘const’?

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is ‘volatile’?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is variable alignment?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the basic data types in C?

A
Integer
floating point
characters
Booleans
Complex Numbers
Enumerations
Pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a pointer?

A

It is a basic data type that contains the address of a variable that has specific value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an array?

A

It is a composite data type that is a consecutive group of data elements of the same type.

17
Q

What is a struct?

A

a struct definition creates a new data type.

18
Q

What is typedef?

A
typedef makes synonyms for previously defined data types.
typedef struct Books {
   char title[50];
   char author[50];
   char subject[100];
   int book_id;
} Book;
int main( ) {
   Book book;
}
19
Q

Given the following code:
struct Time currentTime = {20,15,0};
struct Time * timePtr = &currentTime;
is timePtr ->hour = 12; or timePtr.hour -12 correct?

A

timePtr->hour =12 or (*timePtr).hour =12 would be correct.

But not timePtr.hour

20
Q

What are the drawbacks of structures (composite data type struct)?

A
  • no information hiding: all elements of a struct are public
  • no interface: if implmentation changes, all programs using that struct must change,
  • no inheritance
21
Q

What is Union?

A

Union is a composite data type similar to struct, but data members share memory/storage space. It only contains one data member at a time, which means, if we set one attribute of a union, then the other(s) attribute is set too.
union test {
int x, y;
};

int main() 
{ 
    // A union variable t 
    union test t; 
t.x = 2; // t.y also gets value 2 
printf("After making x = 2:\n x = %d, y = %d\n\n", 
       t.x, t.y); 

t.y = 10; // t.x is also updated to 10 
printf("After making y = 10:\n x = %d, y = %d\n\n", 
       t.x, t.y); 
return 0;  }  output after making x = 2:  x = 2, y = 2 ...after making y = 10: x = 10, y = 10