
If I compile and run this program (as C or C++ - it's a valid program with the same behaviour in both): #include Ĭlearly, the %g output doesn't quite match either the %e or %f output for either x or y above. %g prints the number in the shortest of these two representationsīut this behaviour isn't what I see in reality.

Use the shortest representation: %e or %fĪnd here's a Stack Overflow answer that claims that For instance, at the time of writing this question, says that the g specifier means:

The %g specifier doesn't seem to behave in the way that most sources document it as behaving.Īccording to most sources I've found, across multiple languages that use printf specifiers, the %g specifier is supposed to be equivalent to either %f or %e - whichever would produce shorter output for the provided value.
