title: STM32 printf() redirect
body: <p>I have a STM32VL Discovery board, which uses the STM32F100RB microcontroller. I am using Keil uVision 5.24.2.0. I am using the compiler option 'ARM compiler 'Use default compiler version 5''.</p>
  
  <p>I am trying to figure out how to use/redirect the <code>printf()</code> function on it.</p>
  
  <p>I understand the UART initialisation process, but I am really struggling to understand how to re-direct <code>printf()</code>. I have read multiple sources that I need to.</p>
  
  <p>Consider the following example at <a href="http://www.keil.com/forum/60531/" rel="nofollow noreferrer">http://www.keil.com/forum/60531/</a>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  struct __FILE
  {
    int handle;
    /* Whatever you require here. If the only file you are using is */
    /* standard output using printf() for debugging, no file handling */
    /* is required. */
  };
  
  /* FILE is typedef’d in stdio.h. */
  FILE __stdout;
  
  int fputc(int ch, FILE *f)
  {
    /* Your implementation of fputc(). */
    return ch;
  }
  
  int ferror(FILE *f)
  {
    /* Your implementation of ferror(). */
    return 0;
  }
  
  void test(void)
  {
    printf("Hello world\n");
  }
  </code></pre>
  
  <ol>
  <li>What exactly does <code>__FILE</code> do? I do not see it used.</li>
  <li>Why does <code>__stdout</code> have two '_'s before it?</li>
  <li>Why is the <code>FILE</code> typedef'd data type get assigned to <code>__stdout</code>?</li>
  <li>Does code need to be added to <code>/* Your implementation of fputc(). */</code>?</li>
  </ol>