0
0
Embedded Cprogramming~10 mins

Ring buffer implementation in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the ring buffer size constant.

Embedded C
const int BUFFER_SIZE = [1];
Drag options to blanks, or click blank then click option'
A128
B256
Csize
Dbuffer_size
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a numeric constant.
2fill in blank
medium

Complete the code to declare the ring buffer array.

Embedded C
char ring_buffer[[1]];
Drag options to blanks, or click blank then click option'
ABUFFER_SIZE
Bbuffer
Csize
D128
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined.
3fill in blank
hard

Fix the error in the function to check if the ring buffer is empty.

Embedded C
int is_empty() {
    return [1] == tail;
}
Drag options to blanks, or click blank then click option'
Asize
Bbuffer
Ccount
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing wrong variables like buffer or count.
4fill in blank
hard

Fill both blanks to correctly advance the head index in the ring buffer.

Embedded C
head = (head [1] 1) [2] BUFFER_SIZE;
Drag options to blanks, or click blank then click option'
A+
B-
C%
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or division instead of addition and modulo.
5fill in blank
hard

Fill all three blanks to implement the function that adds a character to the ring buffer safely.

Embedded C
int enqueue(char c) {
    if ((head [1] 1) [2] BUFFER_SIZE == tail) {
        return 0; // Buffer full
    }
    ring_buffer[head] = c;
    head = (head [3] 1) % BUFFER_SIZE;
    return 1;
}
Drag options to blanks, or click blank then click option'
A+
B-
C%
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition and modulo.