C Flashcards

1
Q

What does the volatile keyword do in C?

A

Volatile keyword tells the compiler that the variable can be changed externally (e.g. not via code, like with a GPIO) and keeps the compiler from using optimizations that center around the variable not changing

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

What does the static keyword do in C? What value do they have if not initialized?

A

Static local variables preserve data values between function calls even after they’re out of scope. Static global variables (or functions) can only be accessed in the same file as they’re declared. A static variable is 0 if not initialized and they can only be initialized with constant literals. They are stored in the data segment of memory, not stack.

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

What are malloc() and calloc() in C?

A

malloc() and calloc() are used to allocate dynamic memory (runtime / heap). malloc() takes the number of bytes to allocate as an argument and does not initialize them. calloc() allocates contiguous memory and initializes it. It takes number of blocks and size of each block as an argument. Null return value indicates failure, otherwise a pointer to a block of memory is returned

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

What are tokens in C?

A

Identifiers, Keywords, Constants, Operators, Special Characters, Strings

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

What’s the difference between a global and a static variable in C?

A

Globals are visible to all functions in the same source file. If in a header, visible to all source files that include it. Static variables have local scope and retain state between function calls, useful for counters

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

What is a pointer?

A

Pointers store the address of a variable in memory. They are used primarily to save memory space and speed up execution. They are used to pass arguments by reference, access array elements, return multiple values.

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

What’s the difference between const char* p and char const* p?

A

const char* p is a pointer to a const char* p is a pointer to a const char*, since const char and char const are the same, they are the same.

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

Why does n++ execute faster than = n + 1

A

n++ is a unary operator and n = n + 1 is a binary operator. In modern compilers, n = n + 1 will be converted to n++ automatically

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

What is typecasting and how can it be achieved in C?

A

modifies interpretation of value without changing binary. (type_name) expression. Typecasting can result in loss of data or precision if new type is smaller than old type. Floating point to int would truncate fractional part.

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

What are macros? What are the advantages of macros over functions?

A

Macros are preprocessed and their contents are inserted anywhere the macro name is seen in the code. Macros increase code length over functions, but are faster to execute. Macros don’t check compile-time errors whereas functions do.

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

What are preprocesor directives in C?

A

Macros, include statements, conditional compilation, directives like #ifdef, #pragma

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

What’s #pragma?

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

What’s the memory layout of C programs?

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

What’s a dangling pointer and how is it different from a memory leak?

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

How can you dereference a pointer in C?

A

int* ptr p; declares ptr, &ptr dereferences it accessing the data at the memory location

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

What is a typedef in C?

A

A typedef is a keyword that defines an alias for an existing type. E.g. typedef long lg

17
Q

What’s the difference between type-casting and type-conversion?

A

Type casting is explicit whereas type conversion can be implicit (via compiler) or explicit (via type-casting) type c asting is a type of conversion

18
Q

What is a struct in c?

A

A struct is a user defined composite data type

19
Q

What is a union?

A

a union allows storing different data types in the same location in memory. A union allocates enough memory to hold the largest member of the union. All members share the space. Changing one value can affect the other values

20
Q

What are r-values and l-values?

A

l-values represent memory locations where a value can be stored. r-values are expressions that can be read, not assigned to

21
Q

How can the address of a variable be accessed?

A

int num = 2;
foo( &num );

22
Q

What is a memory leak? How to avoid?

A

When dynamically allocated memory is created but not destroyed, this is a memory leak. Memory is added to the heap but never freed. Call free on memory that is dynamically allocated

23
Q

What are near, far, and huge pointers in C?

A

16bit, 32bit, and

23
Q

What’s the difference between a pointer and a reference?

A

Pointers store the address of a variable in memory. References are an alias to another variable.

char c = ‘z’; // variable
char* char_ptr = *c; // ptr
char& ref = c // reference changing ref would change c and vise-versa
const char& ref = c // const reference, cannot change c through ref but can change c directly which changes ref

23
Q

What does the extern keyword do?

A

extern is a storage specifier that extends visiblity of c variables to other source files. Functions are available by default.

24
Q

Where do globals exist in memory?

A

data segment typically