Test printf implementation

c, portability, portable-applications

Solution

I think Postel's law ("be conservative in what you do, be liberal in what you accept from others") applies here, too. Don't write your tests to require a character-by-character match in order to consider the `printf()` implementation to be working.

Instead, do it a a higher level; parse the text output by `printf()` into the expected datatype, and compare against a value of that type.

I.e., if printing "2.25", parse the text (using `strtod()` or equivalent) and compare against the actual number 2.25, not the literal text string "2.25".

Problem

I would like to have a portable implemenation of my application. However, I have heard that there are some issues with printf from the stdlib on certain machines where it does not behave as intended. For instance, when using the conversion specifier %f then it can happen that on certain architectures the printf implementation includes a decimal point in the output! Now I am wondering, if there are maybe some testing routines out there which I could use to test the semantic correctness of stdlib c implementation, in particular the printf routine. Maybe there are some good resources that point out some issues when porting programs? Many thanks, Heinz

Original source