0
0
Embedded Cprogramming~20 mins

Printf redirect to UART in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UART Printf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AUART_Transmit called with 'H' only
BHello UART!\n
CNo output, printf not redirected
DCompilation error due to missing function
Attempts:
2 left
💡 Hint
Think about how printf uses putchar internally and how __io_putchar redirects output.
🧠 Conceptual
intermediate
1: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?
Aprintf_uart
BUART_Init
Cmain
D__io_putchar
Attempts:
2 left
💡 Hint
This function is called internally by printf to output each character.
🔧 Debug
advanced
2: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;
}
ABecause __io_putchar returns 0 instead of ch, printf buffering fails
BUART_SendChar is empty and does not send data
Cprintf requires UART_Init call before use
DMissing #include <stdint.h> causes failure
Attempts:
2 left
💡 Hint
Check the return value of __io_putchar and how printf uses it.
📝 Syntax
advanced
2: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.
Aint __io_putchar(int ch) { UART_Send((char)ch); return ch; }
Bvoid __io_putchar(int ch) { UART_Send(ch); }
Cint __io_putchar(char ch) { UART_Send(ch); return ch; }
Dint __io_putchar(int ch) { UART_Send(ch); return 0; }
Attempts:
2 left
💡 Hint
Check function return type and parameter types carefully.
🚀 Application
expert
3: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;
}
A11
B10
C12
D13
Attempts:
2 left
💡 Hint
Count all characters including newline characters in the string.