Challenge - 5 Problems
UART Printf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this UART printf redirection code?
Consider the following embedded C code that redirects printf output to UART. What will be printed on the UART when the main function runs?
Embedded C
#include <stdio.h> #include <stdint.h> // Simulated UART transmit function void UART_Transmit(char c) { // In real hardware, this sends the char over UART putchar(c); // For simulation, print to stdout } // Redirect putchar to UART_Transmit int __io_putchar(int ch) { UART_Transmit((char)ch); return ch; } int main() { printf("Hello UART!\n"); return 0; }
Attempts:
2 left
💡 Hint
Think about how printf uses putchar internally and how __io_putchar redirects output.
✗ Incorrect
The __io_putchar function is used by printf to output each character. Since __io_putchar calls UART_Transmit, which sends characters to UART (simulated here by putchar), the entire string "Hello UART!\n" is printed.
🧠 Conceptual
intermediate1:30remaining
Which function is typically overridden to redirect printf output to UART?
In embedded C, to redirect printf output to UART, which function do you usually need to implement or override?
Attempts:
2 left
💡 Hint
This function is called internally by printf to output each character.
✗ Incorrect
The __io_putchar function is called by printf to output characters. Overriding it to send characters via UART redirects printf output to UART.
🔧 Debug
advanced2:30remaining
Why does this UART printf redirection code fail to output anything?
Examine the code below. Why does printf not send any output to UART?
Embedded C
#include <stdio.h> void UART_SendChar(char c) { // UART send implementation } int __io_putchar(int ch) { UART_SendChar(ch); return 0; } int main() { printf("Test\n"); return 0; }
Attempts:
2 left
💡 Hint
Check the return value of __io_putchar and how printf uses it.
✗ Incorrect
printf expects __io_putchar to return the character written. Returning 0 breaks printf's internal buffering, so no output appears.
📝 Syntax
advanced2:00remaining
Which option correctly implements __io_putchar for UART redirection?
Select the correct implementation of __io_putchar that sends a character over UART and returns the character.
Attempts:
2 left
💡 Hint
Check function return type and parameter types carefully.
✗ Incorrect
Option A matches the expected signature and returns the character. Option A has wrong return type, C has wrong parameter type, A returns 0 which breaks printf.
🚀 Application
expert3:00remaining
How many characters are sent over UART by this code?
Given the code below, how many characters will UART_Transmit send when main runs?
Embedded C
#include <stdio.h> void UART_Transmit(char c) { // Sends character over UART } int __io_putchar(int ch) { UART_Transmit((char)ch); return ch; } int main() { printf("Line1\nLine2\n"); return 0; }
Attempts:
2 left
💡 Hint
Count all characters including newline characters in the string.
✗ Incorrect
The string "Line1\nLine2\n" has 5 characters + 1 newline + 5 characters + 1 newline = 12 characters total sent.