Printf: Differences in Java and C Implementations

Today I noticed that I cannot use * to pass width or precision arguments to the Java printf implementation.

Thus, the following printf arguments are valid in C, but not in Java:

 "%*d", 10, 3 "%-*d", 10, 3 "%0*d", 10, 3 "%*.5f", 11, 1.0/9 "%-11.*f", 5, 1.0/9 "%0*.*f", 11, 5, 1.0/9 

Are there other implementation differences that I should be aware of?

+2
source share
2 answers

I would not think of it as differences. I just read the Java documentation carefully and completely based on that. Thinking in terms of differences is likely to slip through the network.

In other words, I would consider the similarities as coincidences and assume that the two are different until proven otherwise :)

+12
source

One difference hit me recently: when printing a floating-point number% g (automatically select% e or% f), the precision flag is different:

 Java: precision = "number of fractional digits after the decimal point" (%f, %e) "total number of digits in the resulting magnitude after rounding" (%g %G) C: precision = "number of fractional digits after the decimal point" (%f %e ) "maximum number of significant digits" (%g %G) 
+1
source

All Articles