Bird
Raised Fist0
Embedded Cprogramming~20 mins

Printf debugging over UART in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
UART Debugging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of UART printf with buffer overflow
Consider this embedded C code snippet that sends debug messages over UART using printf. What will be the output on the UART console?
Embedded C
#include <stdio.h>
#include <string.h>

char uart_buffer[10];

int uart_send(const char *msg) {
    // Simulate UART send by copying to buffer
    strcpy(uart_buffer, msg);
    return 0;
}

int main() {
    char debug_msg[20];
    sprintf(debug_msg, "Value: %d", 12345);
    uart_send(debug_msg);
    printf("UART buffer: %s\n", uart_buffer);
    return 0;
}
AUART buffer: Value: 123456789
BUART buffer: Value: 1234
CUART buffer: Value: 12345
DRuntime error due to buffer overflow
Attempts:
2 left
💡 Hint
Remember that strcpy copies until the null terminator, but uart_buffer is only 10 bytes.
🧠 Conceptual
intermediate
1:30remaining
Why use printf debugging over UART in embedded systems?
Which of the following is the main reason developers use printf debugging over UART in embedded systems?
ATo increase the speed of the embedded program execution
BTo visually see variable values and program flow without a debugger
CTo permanently store debug logs in non-volatile memory
DTo reduce power consumption during debugging
Attempts:
2 left
💡 Hint
Think about what printf debugging helps you understand during development.
🔧 Debug
advanced
2:00remaining
Identify the cause of missing UART output
This embedded C code is supposed to send a debug message over UART using printf. However, no output appears on the UART console. What is the most likely cause?
Embedded C
#include <stdio.h>

void uart_init() {
    // UART initialization code
}

int uart_putchar(char c) {
    // Send character over UART hardware
    return 0;
}

int main() {
    uart_init();
    printf("Debug: %d\n", 42);
    return 0;
}
AThe main function should return void, not int
BUART hardware is not initialized properly
CThe uart_putchar function is missing a return statement
DThe printf output is buffered and not flushed to UART
Attempts:
2 left
💡 Hint
Think about how printf sends characters and when output appears on UART.
📝 Syntax
advanced
2:30remaining
Correct UART printf redirection code
Which option correctly implements the function to redirect printf output to UART by overriding the _write syscall in embedded C?
Embedded C
int _write(int file, char *ptr, int len) {
    for (int i = 0; i < len; i++) {
        uart_send_char(ptr[i]);
    }
    return len;
}
A
int _write(int file, char *ptr, int len) {
    for (int i = 0; i &lt; len; i++) {
        uart_send_char(ptr[i]);
    }
    return len;
}
B
int _write(int file, char ptr[], int len) {
    int i = 0;
    while (ptr[i] != '\0') {
        uart_send_char(ptr[i]);
        i++;
    }
    return i;
}
C
void _write(int file, char *ptr, int len) {
    for (int i = 0; i &lt; len; i++) {
        uart_send_char(ptr[i]);
    }
}
D
int _write(int file, char *ptr, int len) {
    uart_send_string(ptr);
    return len;
}
Attempts:
2 left
💡 Hint
The _write function must return the number of bytes written and send exactly len bytes.
🚀 Application
expert
3:00remaining
Calculate UART debug message transmission time
An embedded system sends debug messages over UART at 115200 baud rate using printf. If a debug message is 50 characters long, approximately how long in milliseconds will it take to transmit the entire message? Assume 1 start bit, 8 data bits, no parity, and 1 stop bit per character.
AApproximately 4.35 ms
BApproximately 0.43 ms
CApproximately 8.7 ms
DApproximately 17.4 ms
Attempts:
2 left
💡 Hint
Calculate bits per character and total bits, then divide by baud rate.

Practice

(1/5)
1. What is the main purpose of using printf debugging over UART in embedded C?
easy
A. To store data permanently in memory
B. To increase the speed of the embedded device
C. To send debug messages from the embedded device to a computer or terminal
D. To control hardware pins directly

Solution

  1. Step 1: Understand UART communication

    UART allows serial communication between the embedded device and external devices like a PC.
  2. Step 2: Role of printf debugging

    Using printf over UART sends text messages to the PC for debugging and monitoring program behavior.
  3. Final Answer:

    To send debug messages from the embedded device to a computer or terminal -> Option C
  4. Quick Check:

    UART + printf = debug messages [OK]
Hint: Printf over UART sends messages to PC for debugging [OK]
Common Mistakes:
  • Thinking printf speeds up the device
  • Confusing UART with memory storage
  • Assuming printf controls hardware pins
2. Which of the following is the correct way to redirect printf output to UART in embedded C?
easy
A. Call printf directly without UART initialization
B. Set a GPIO pin high to enable printf
C. Use scanf to read UART data
D. Implement a custom _write function that sends characters via UART

Solution

  1. Step 1: Understand printf redirection

    Standard printf sends output to stdout, which must be redirected to UART by implementing a low-level write function.
  2. Step 2: Custom _write function role

    Implementing _write sends each character to UART transmit, enabling printf output over UART.
  3. Final Answer:

    Implement a custom _write function that sends characters via UART -> Option D
  4. Quick Check:

    Redirect printf by custom _write [OK]
Hint: Redirect printf by writing _write function sending UART chars [OK]
Common Mistakes:
  • Calling printf without UART setup
  • Using scanf instead of printf for output
  • Trying to enable printf by GPIO pin
3. Given the code below, what will be the output sent over UART?
void UART_SendChar(char c);

int _write(int file, char *ptr, int len) {
  for (int i = 0; i < len; i++) {
    UART_SendChar(ptr[i]);
  }
  return len;
}

int main() {
  printf("Temp: %d C\n", 25);
  return 0;
}
medium
A. Temp: 25 C\n
B. Temp: %d C\n
C. Temp: 25 C
D. No output because UART_SendChar is not defined

Solution

  1. Step 1: Understand _write function

    The _write function sends each character of the printf string to UART_SendChar, which transmits it over UART.
  2. Step 2: Analyze printf output

    printf formats the string "Temp: 25 C\n" and _write sends it character by character.
  3. Final Answer:

    Temp: 25 C\n -> Option A
  4. Quick Check:

    Formatted printf string sent via UART [OK]
Hint: Printf formats string, _write sends chars via UART [OK]
Common Mistakes:
  • Thinking %d prints literally
  • Ignoring newline character
  • Assuming UART_SendChar undefined means no output
4. What is the main problem in this code snippet for printf debugging over UART?
void UART_SendChar(char c) {
  while (!(UART->SR & UART_SR_TXE));
  UART->DR = c;
}

int _write(int file, char *ptr, int len) {
  for (int i = 0; i < len; i++) {
    UART_SendChar(ptr[i]);
  }
  return len;
}

int main() {
  printf("Hello UART\n");
  return 0;
}
medium
A. UART peripheral is not initialized before use
B. UART_SendChar does not wait for TXE flag
C. _write function returns wrong length
D. printf is not called in main

Solution

  1. Step 1: Check UART_SendChar function

    UART_SendChar waits for TXE flag and writes data, so it is correct.
  2. Step 2: Check UART initialization

    The code does not show UART initialization, which is required before sending data.
  3. Final Answer:

    UART peripheral is not initialized before use -> Option A
  4. Quick Check:

    Missing UART init causes no transmission [OK]
Hint: Always initialize UART before sending data [OK]
Common Mistakes:
  • Ignoring UART initialization step
  • Thinking TXE flag wait is missing
  • Believing _write return value is incorrect
5. You want to debug a sensor reading using printf over UART. The sensor value is an integer that can be zero or negative. Which printf format specifier and UART setup is best to correctly display the value including negatives?
hard
A. Use %f in printf without UART redirection
B. Use %d in printf and ensure UART is initialized and _write redirects output
C. Use %x in printf and send data directly to GPIO pins
D. Use %u in printf and skip UART initialization

Solution

  1. Step 1: Choose correct format specifier

    %d prints signed integers including zero and negatives correctly, unlike %u (unsigned) or %x (hex).
  2. Step 2: Ensure UART setup

    UART must be initialized and printf output redirected via _write to UART for messages to appear on terminal.
  3. Final Answer:

    Use %d in printf and ensure UART is initialized and _write redirects output -> Option B
  4. Quick Check:

    Signed int format + UART init + _write = correct debug [OK]
Hint: Use %d and proper UART init with _write for signed values [OK]
Common Mistakes:
  • Using %u which can't show negatives
  • Skipping UART initialization
  • Sending data to GPIO instead of UART
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard