C programming skills ChatGPT Flashcards

(56 cards)

1
Q

What is the difference between ++i and i++ in C?

A

++i increments i before its value is used. i++ increments i after its value is used.

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

What is a pointer in C?

A

A pointer is a variable that stores the memory address of another variable.

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

What is a segmentation fault?

A

A segmentation fault occurs when a program tries to access memory it shouldn’t, like dereferencing a null or invalid pointer.

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

What is the use of volatile keyword?

A

volatile tells the compiler not to optimize the variable, as it may change unexpectedly (e.g., by hardware).

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

What are the storage classes in C?

A

auto, register, static, and extern.

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

What are the main differences between a microcontroller and a microprocessor?

A

Microcontrollers have built-in memory and peripherals; microprocessors require external components.

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

What is the purpose of const keyword in embedded C?

A

const defines a variable whose value cannot be changed after initialization, useful for read-only memory mapping.

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

What is the difference between volatile and const?

A

const prevents modification, while volatile tells the compiler not to optimize because the value can change externally.

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

What is the size of int, short, long and long long in C?

A

Typically: short = 2 bytes, int = 4 bytes, long = 4 bytes, long long = 8 bytes (but may vary by system).

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

How do you declare an interrupt service routine (ISR) in C?

A

Use the appropriate compiler-specific keyword or attribute (e.g., \_\_attribute\_\_((interrupt)) or ISR(vector)).

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

What is a memory-mapped I/O?

A

Memory-mapped I/O uses specific memory addresses to interact with hardware registers.

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

What is the use of bit fields in C?

A

Bit fields allow the packing of data in a struct to use less memory, useful for hardware register definitions. E.g:

struct StatusRegister {
unsigned int ready : 1;
unsigned int error : 1;
unsigned int code : 4;
};

for:
Memory-efficient storage
Representing hardware registers or flags

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

What is a watchdog timer?

A

A hardware timer that resets the system if the software fails to periodically reset it.

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

What are static variables in C?

A

static variables retain their value between function calls and have internal linkage when used at file scope.

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

What is the difference between malloc and calloc?

A

malloc allocates uninitialized memory, calloc allocates and zeroes the memory.

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

Why is dynamic memory allocation discouraged in embedded systems?

A

It may cause fragmentation, unpredictable allocation times, and memory leaks.

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

What is endianess?

A

The order in which bytes are stored for multi-byte data types: Little Endian (LSB first), Big Endian (MSB first).

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

What are the different types of timers in microcontrollers?

A

Basic timers, general-purpose timers, and advanced-control timers (varies by architecture).

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

What is polling vs. interrupt?

A

Polling continuously checks for an event; interrupts respond asynchronously when an event occurs.

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

What is a circular buffer?

A

A circular buffer is a fixed-size buffer that wraps around to the beginning when the end is reached, ideal for streaming data.

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

What is a race condition?

A

A race condition occurs when two threads or ISRs access shared data concurrently and the outcome depends on timing.

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

What is reentrant code?

A

Code that can be safely interrupted and re-entered before the previous execution is complete.

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

What are the types of memory in an embedded system?

A

ROM, RAM, Flash, EEPROM, and external memory depending on architecture.

24
Q

What is the use of assert() in embedded C?

A

assert() is used to catch programming errors by verifying assumptions at runtime, typically during development.

25
How would you debounce a button in software?
Use a delay loop, a counter-based approach, or a timer interrupt to confirm stable input over time.
26
What is the difference between `struct` and `union`?
`struct` allocates memory for all members; `union` shares memory between all members.
27
What is the stack and heap?
Stack is LIFO memory for function calls and local variables. Heap is dynamically allocated memory.
28
How do you avoid race conditions in embedded systems?
Use critical sections, mutexes, disabling interrupts, or atomic operations.
29
What is a NULL pointer?
A NULL pointer points to no valid memory location. It’s used to indicate that the pointer is not assigned.
30
What is the difference between `typedef` and `#define`?
`typedef` gives a new name to a type, `#define` replaces text via the preprocessor.
31
How can you measure the execution time of a function?
Use hardware timers or read CPU cycle counters before and after execution.
32
What is ISR latency?
The time between the hardware interrupt and the start of the ISR execution.
33
How do you ensure atomicity in embedded systems?
By disabling interrupts or using atomic built-in functions or critical sections.
34
What is an RTOS and why is it used?
An RTOS manages multiple tasks in real-time, ensuring predictable and timely task execution.
35
What is stack overflow and how can it be prevented?
Stack overflow happens when stack memory exceeds its limit. Prevent it by proper stack size allocation and avoiding deep recursion.
36
What is the difference between hard real-time and soft real-time systems?
Hard real-time systems must meet deadlines strictly; soft real-time systems can tolerate occasional deadline misses.
37
How can you detect a memory leak in an embedded system?
Track allocations and deallocations manually or with debug tools like heap usage monitors.
38
What is a semaphore?
A synchronization tool used to manage resource access between concurrent tasks.
39
What is the role of `extern` keyword?
`extern` declares a variable that is defined in another file or scope.
40
What are memory barriers?
Instructions that enforce memory access ordering across CPU and compiler optimizations.
41
What are interrupts priorities?
Priorities define which interrupt gets handled first when multiple occur simultaneously.
42
What is DMA (Direct Memory Access)?
DMA allows peripherals to access system memory directly, offloading the CPU.
43
What is double buffering?
A technique where data is written to one buffer while the other is being read to avoid tearing or loss.
44
How do you prevent buffer overflows?
By checking bounds, using safe string functions, and limiting input sizes.
45
What is a bootloader?
A small program that loads the main firmware into memory after power-on.
46
What is link-time vs. run-time?
Link-time refers to the time code is compiled and linked; run-time is during actual execution.
47
What are linker scripts used for?
To define memory layout and placement of code/data sections in embedded systems.
48
What is the purpose of `#pragma` directives?
`#pragma` provides special instructions to the compiler (e.g., to suppress warnings or align data).
49
What is `volatile const` and when is it used?
It’s for read-only variables that can change outside the program, such as hardware status registers.
50
What is the difference between task and thread?
In embedded, a task is a schedulable unit in an RTOS; a thread is typically a concept in multithreading.
51
What is interrupt nesting?
Allowing higher-priority interrupts to interrupt lower-priority ISRs.
52
How can you reduce power consumption in embedded C?
Use sleep modes, reduce clock frequency, optimize code, disable unused peripherals.
53
What does it mean when a compiler optimizes a variable?
It means the compiler assumes the variable doesn't change unexpectedly, so it may cache its value in a register or eliminate reads/writes to improve performance.
54
Why is `volatile` necessary in embedded systems?
`volatile` ensures the compiler always reads the variable from memory. This is critical for variables modified by hardware (I/O registers), ISRs, or other threads.
55
What is the difference between storage classes in C (auto, register, static, and extern)?
| Storage Class | Scope | Lifetime | Stored in | Notes | `auto` | Local (block) | Until block ends | RAM | Default for local variables; rarely used explicitly | | `register` | Local (block) | Until block ends | CPU register | Suggests fast access; cannot take address with `&` | | `static` | File or block | Entire program run | RAM or Flash | Keeps value between calls; file scope if declared outside functions | | `extern` | Global | Entire program run | RAM or Flash | Declares a global variable defined elsewhere |
56
What is the difference between basic, general-purpose, and advanced-control timers?
| Timer Type | Features | Use Cases | **Basic Timer** | Simple up/down counting, no input/output pins | Time base generation, delays | | **General-Purpose** | Input capture, output compare, PWM, interrupts | Measuring pulse width, PWM control | | **Advanced-Control** | All general features + dead-time, synchronization | Motor control, complex PWM, safety |