Bird
Raised Fist0
Embedded Cprogramming~10 mins

Printf redirect to UART in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Printf redirect to UART
Start
↓
Call printf
↓
printf calls _write
↓
_write sends data to UART
↓
UART transmits data
↓
Data appears on terminal
↓
End
When printf is called, it internally calls the _write function, which we redirect to send characters over UART, so output appears on a terminal.
Execution Sample
Embedded C
int _write(int file, char *ptr, int len) {
  for (int i = 0; i < len; i++) {
    UART_SendChar(ptr[i]);
  }
  return len;
}
This code redirects printf output by sending each character to UART.
Execution Table
StepActionInputUART_SendChar Called WithReturn Value
1printf called"Hello\n"
2printf calls _write"Hello\n", length=6
3Loop i=0ptr[0] = 'H''H'
4Loop i=1ptr[1] = 'e''e'
5Loop i=2ptr[2] = 'l''l'
6Loop i=3ptr[3] = 'l''l'
7Loop i=4ptr[4] = 'o''o'
8Loop i=5ptr[5] = '\n''\n'
9_write returns6
10printf completes
💡 All characters sent to UART, _write returns length, printf finishes.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
i01234566
ptr[i]'H''e''l''l''o''\n'
Key Moments - 3 Insights
Why does printf call _write instead of sending characters directly?
printf uses _write as a low-level output function. Redirecting _write lets us control where printf sends its output, as shown in steps 2-9 of the execution_table.
What happens if UART_SendChar is not implemented correctly?
If UART_SendChar fails, characters won't be sent, so output won't appear on the terminal. The loop in steps 3-8 depends on UART_SendChar working properly.
Why does _write return the length of characters sent?
_write returns the number of characters sent to signal success to printf, as seen in step 9. This tells printf all data was handled.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what character does UART_SendChar send at step 5?
A'l'
B'H'
C'e'
D'o'
💡 Hint
Check the 'UART_SendChar Called With' column at step 5 in execution_table.
At which step does _write finish sending all characters?
AStep 8
BStep 9
CStep 10
DStep 6
💡 Hint
Look for when _write returns in the execution_table.
If the input string length was 4 instead of 6, how would the variable 'i' final value change?
AFinal value would be 6
BFinal value would be 0
CFinal value would be 4
DFinal value would be 5
💡 Hint
Check variable_tracker for 'i' values and relate to string length.
Concept Snapshot
Redirect printf output by implementing _write function.
_write sends each character to UART using UART_SendChar.
printf calls _write internally to output data.
Return number of characters sent from _write.
This lets printf output appear on UART terminal.
Full Transcript
When you use printf in embedded C, it doesn't send characters directly. Instead, it calls a function named _write. By writing your own _write function, you can send each character to UART using UART_SendChar. This way, all printf output goes through UART and appears on your terminal. The _write function loops over each character in the string, sends it via UART, and returns the total number of characters sent. This process is shown step-by-step in the execution table, tracking each character sent and the loop variable. Understanding this flow helps you redirect printf output to UART easily.

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