title: How to represent currency or money in C
body: <p><strong>TL;DR</strong><sup>1</sup> What is an accurate and maintainable approach for representing currency or money in C?</p>
  
  <hr>
  
  <p><strong><em>Background to the question:</em></strong><br>
  This has been answered for a number of other languages, but I could not find a solid answer for the C language.</p>
  
  <ul>
  <li><p><strong>C#</strong> <a href="https://stackoverflow.com/q/1008826/1345223">What data type should I use to represent money in C#?</a></p></li>
  <li><p><strong>Java</strong> <a href="https://stackoverflow.com/q/3730019/1345223">Why not use Double or Float to represent currency?</a></p></li>
  <li><p><strong>Objective-C</strong> <a href="https://stackoverflow.com/q/830369/1345223">How to represent money in Objective-C / iOS?</a></p></li>
  </ul>
  
  <p><sub><em>Note: There's plenty more similar questions for other languages, I just pulled a few for representational purposes.</em></sub></p>
  
  <p>All of those questions can be distilled down to "use a <code>decimal</code> data type" where the specific type may vary based upon the language.</p>
  
  <p>There is a <a href="https://stackoverflow.com/q/8886400/1345223">related question</a> that ends up suggesting using a "fixed point" approach, but none of the answers address using a specific data type in C.</p>
  
  <p>Likewise, I have looked at arbitrary precision libraries such as <a href="https://gmplib.org/" rel="noreferrer">GMP</a>, but it's not clear to me if this is the best approach to use or not.</p>
  
  <hr>
  
  <p><strong><em>Simplifying Assumptions:</em></strong>  </p>
  
  <ul>
  <li><p>Presume an x86 or x64 based architecture, but please call out any assumptions that would impact a RISC based architecture such as a Power chip or an Arm chip.</p></li>
  <li><p>Accuracy in calculations is the primary requirement.  <a href="https://softwareengineering.stackexchange.com/q/141005/53019">Ease of maintenance</a> would be the next requirement.  Speed of calculations is important, but is tertiary to the other requirements.</p></li>
  <li><p>Calculations need to be able to safely support operations accurate to the <a href="https://en.wikipedia.org/wiki/Mill_%28currency%29" rel="noreferrer">mill</a> as well as supporting values ranging up to the trillions (10^9)</p></li>
  </ul>
  
  <hr>
  
  <p><strong><em>Differences from other questions:</em></strong>  </p>
  
  <p>As noted above, this type of question has been asked before for multiple other languages.  This question is different from the other questions for a couple of reasons.</p>
  
  <p>Using the accepted answer from: <a href="https://stackoverflow.com/q/3730019/1345223">Why not use Double or Float to represent currency?</a>, let's highlight the differences.</p>
  
  <blockquote>
    <p>(<strong>Solution 1</strong>) A solution that works in just about any language is to use integers instead, and count cents. For instance, 1025 would be $10.25. Several languages also have built-in types to deal with money. (<strong>Solution 2</strong>) Among others, Java has the BigDecimal class, and C# has the decimal type.</p>
  </blockquote>
  
  <p><sub><em>Emphasis added to highlight the two suggested solutions</em></sub></p>
  
  <p>The first solution is essentially a variant of the "fixed point" approach.  There is a problem with this solution in that the suggested range (tracking cents) is insufficient for mill based calculations and significant information will be lost on rounding.  </p>
  
  <p>The other solution is to use a native <code>decimal</code> class which is not available within C.</p>
  
  <p>Likewise, the answer doesn't consider other options such as creating a struct for handling these calculations or using an arbitrary precision library.  Those are understandable differences as Java doesn't have structs and why consider a 3rd party library when there is native support within the language.</p>
  
  <p>This question is different from that question and other related questions because C doesn't have the same level of native type support and has language features that the other languages don't.  And I haven't seen any of the other questions address the multiple ways that this could be approached in C.</p>
  
  <hr>
  
  <p><strong><em>The Question:</em></strong><br>
  From my research, it seems that <code>float</code> is not an appropriate data type to use to represent currency within a C program due to floating point error.  </p>
  
  <p>What should I use to represent money in C, and why is that approach better than other approaches?</p>
  
  <p><sup>1</sup> <sub><em>This question started in a shorter form, but feedback received indicated the need to clarify the question.</em></sub></p>