Bird
Raised Fist0
Embedded Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of overriding the fputc function in embedded C when redirecting printf output to UART?
easy
A. To clear the UART receive buffer
B. To change the baud rate of the UART communication
C. To initialize the UART peripheral before use
D. To send each character from printf to the UART hardware for transmission

Solution

  1. Step 1: Understand printf output mechanism

    The printf function sends characters one by one to fputc for output.
  2. Step 2: Role of overriding fputc

    By overriding fputc, we redirect each character to UART transmit function, sending it out via UART.
  3. Final Answer:

    To send each character from printf to the UART hardware for transmission -> Option D
  4. Quick Check:

    Overriding fputc redirects output to UART [OK]
Hint: Remember: printf calls fputc for output [OK]
Common Mistakes:
  • Confusing fputc with UART initialization
  • Trying to change UART settings inside fputc
  • Ignoring that printf uses fputc internally
2. Which of the following is the correct prototype for overriding fputc to redirect printf output to UART in embedded C?
easy
A. char fputc(int ch, FILE *f);
B. void fputc(char ch);
C. int fputc(FILE *f, char ch);
D. int fputc(char ch, FILE *f);

Solution

  1. Step 1: Recall standard fputc signature

    The standard fputc function prototype is int fputc(int ch, FILE *stream);.
  2. Step 2: Match options with correct signature

    char fputc(int ch, FILE *f); matches the correct order and types: returns char (though standard is int), takes int and FILE pointer.
  3. Final Answer:

    char fputc(int ch, FILE *f); -> Option A
  4. Quick Check:

    Correct fputc prototype returns int and takes int, FILE* [OK]
Hint: Prototype must match standard fputc signature [OK]
Common Mistakes:
  • Swapping parameter order
  • Using void return type
  • Using wrong parameter types
3. Given the following code snippet, what will be the output on the UART terminal?
int fputc(int ch, FILE *f) {
    UART_SendChar((char)ch);
    return ch;
}

int main() {
    printf("Hi\n");
    return 0;
}
medium
A. Only 'H' is sent to UART
B. No output because UART_SendChar is not defined
C. Hi followed by a newline on UART terminal
D. Compilation error due to missing UART initialization

Solution

  1. Step 1: Understand fputc role in printf

    Each character of "Hi\n" is sent to fputc, which calls UART_SendChar to send it.
  2. Step 2: Output on UART terminal

    Characters 'H', 'i', and newline '\n' are sent sequentially, so "Hi" and a newline appear on UART terminal.
  3. Final Answer:

    Hi followed by a newline on UART terminal -> Option C
  4. Quick Check:

    printf output redirected via fputc to UART [OK]
Hint: Each printf char calls fputc, which sends via UART [OK]
Common Mistakes:
  • Assuming no output without UART initialization code shown
  • Thinking only first char is sent
  • Confusing compile errors with runtime behavior
4. Identify the error in this fputc override code that tries to redirect printf to UART:
int fputc(int ch, FILE *f) {
    UART_SendChar(ch);
    return 0;
}
medium
A. Returning 0 instead of the character causes incorrect printf behavior
B. Passing ch directly to UART_SendChar causes type mismatch
C. Missing UART initialization before sending characters
D. Function should return void, not int

Solution

  1. Step 1: Check return value of fputc

    The fputc function must return the character written on success, or EOF on failure.
  2. Step 2: Analyze the code's return statement

    Returning 0 (null char) instead of the character can cause printf to misbehave or stop output prematurely.
  3. Final Answer:

    Returning 0 instead of the character causes incorrect printf behavior -> Option A
  4. Quick Check:

    Return written char from fputc to keep printf working [OK]
Hint: Always return the character from fputc [OK]
Common Mistakes:
  • Returning 0 instead of the character
  • Ignoring return value importance
  • Assuming void return type is allowed
5. You want to redirect printf output to UART and also support multiple UART ports. Which approach correctly extends fputc to select UART port dynamically?
hard
A. Create separate fputc functions for each UART port and call them manually
B. Use a global variable to store current UART port and send chars accordingly inside fputc
C. Modify printf to accept UART port as parameter
D. Use interrupts to switch UART ports automatically during fputc

Solution

  1. Step 1: Understand fputc limitation

    Only one fputc function can be linked, so multiple versions are not possible.
  2. Step 2: Use global variable for UART selection

    By storing the current UART port in a global variable, fputc can send characters to the selected UART dynamically.
  3. Step 3: Evaluate other options

    Modifying printf is complex; interrupts do not switch ports automatically; multiple fputc functions cause conflicts.
  4. Final Answer:

    Use a global variable to store current UART port and send chars accordingly inside fputc -> Option B
  5. Quick Check:

    Global UART selector enables dynamic port choice in fputc [OK]
Hint: Use global UART variable to select port inside single fputc [OK]
Common Mistakes:
  • Trying multiple fputc functions
  • Changing printf signature
  • Relying on interrupts to switch UART ports
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