title: Copying 0 as 000 in C++?
body: <p>This question is ANSWERED. Nothing to do with formatters but instead my idiocy when it comes to copying to new buffers.</p>
  
  <hr>
  
  <p>I'm hoping this is a one line answer. I have a <code>snprintf()</code> statement which is something like the following:</p>
  
  <pre><code>snprintf(buffer, sizeof(buffer), "%03d", 0U);
  </code></pre>
  
  <p>I'm expecting <code>buffer</code> to hold <code>000</code> but for some reason it only holds <code>00</code>. Assume the buffer is plenty large enough to hold what I want. Am I being silly?</p>
  
  <p>EDIT:</p>
  
  <p>See below for complete code with context. I was trying to simplify it before as I didn't think all this context was necessary. The point still remains, using <code>%04u</code> gives me <code>000</code> in the first CSV row. <code>%03u</code> only gives me <code>00</code>.</p>
  
  <pre><code>uint16_t CSVGenerator::write_csv_data(TestRecord* record)
  {
      // Define the templates.
      const char *row_template = "%04u,%6.3E,%6.3E,%6.3E,%6.3E,%6.3E\n";
      char csv_row_buffer[CSV_ROW_BUFFER_SIZE];
  
      // Add the data.
      uint16_t row_count = 0U;
     for (uint16_t reading = 0U; reading &lt; MEASURE_READING_COUNT; ++reading)
     {
         // Parse the row.
         snprintf(csv_row_buffer, sizeof(csv_row_buffer), row_template,
                 // Test ID
                 MEASURE_PERIOD_SECS * reading,
                 // Impedances Z1-Z5.
                 record-&gt;measurements[reading][0U],
                 record-&gt;measurements[reading][1U],
                 record-&gt;measurements[reading][2U],
                 record-&gt;measurements[reading][3U],
                 record-&gt;measurements[reading][4U]);
  
         // Add it to the main buffer, excluding the terminator.
         strncpy((m_csv_data_buffer + (reading * CSV_ROW_BUFFER_SIZE) - 1U),
                 csv_row_buffer, (sizeof(csv_row_buffer) - 1U));
  
         // Increment the row count.
         ++row_count;
     } // for : each reading.
  
     return row_count;
  }
  </code></pre>