The following should line everything up correctly:
printf("ABC %6.1f DEF\n", test);
printf("ABC %6.1f DEF\n", test2);
When I run this, I get:
ABC 1234.5 DEF
ABC 14.5 DEF
The issue is that, in %5.1f
, the 5
is the number of characters allocated for the entire number, and 1234.5
takes more than five characters. This results in misalignment with 14.5
, which does fit in five characters.
Related Contents:
- Correct format specifier for double in printf
- What does the constant 0.0039215689 represent?
- How do I restrict a float value to only two places after the decimal point in C?
- ‘float’ vs. ‘double’ precision
- Why doesn’t C have unsigned floats?
- Printf width specifier to maintain precision of floating-point value
- What’s the difference between hard and soft floating point numbers?
- C: printf a float value
- Checking if float is an integer
- Double cast to unsigned int on Win32 is truncating to 2,147,483,648
- What’s the use of suffix `f` on float value
- Make C floating point literals float (rather than double)
- How to get the sign, mantissa and exponent of a floating point number
- Why wasn’t a specifier for `float` defined in `printf`?
- How to extract the decimal part from a floating point number in C?
- What is the difference between casting to `float` and adding `f` as a suffix when initializing a `float`?
- Why does MSVS not optimize away +0? [duplicate]
- How to convert string to float?
- How are floating point numbers stored in memory?
- How does this float square root approximation work?
- How to generate random float number in C
- Is there a document describing how Clang handles excess floating-point precision?
- Nonintuitive result of the assignment of a double precision number to an int variable in C
- How does printf and co differentiate between float and double
- C: How to wrap a float to the interval [-pi, pi)
- Efficient floating-point division with constant integer divisors
- C convert floating point to int
- floating point multiplication vs repeated addition
- #pragma pack effect
- How to measure time in milliseconds using ANSI C?
- What’s the difference between a file descriptor and file pointer?
- Using printf with a non-null terminated string
- What does -D_XOPEN_SOURCE do/mean?
- Is it safe to check floating point values for equality to 0?
- What is the rationale for fread/fwrite taking size and count as arguments?
- Why do most C developers use define instead of const? [duplicate]
- Platform independent size_t Format specifiers in c?
- How to find the size of an array (from a pointer pointing to the first element array)?
- Is using flexible array members in C bad practice?
- Pthreads vs. OpenMP
- What is the behavior of printing NULL with printf’s %s specifier?
- How was the first C compiler written?
- Linux shared memory: shmget() vs mmap()?
- How Does sizeof(Array) work
- atol() v/s. strtol()
- What is the maximum length in chars needed to represent any double value?
- concatenate char array in C
- ‘\0’ evaluates false, “\0” evaluates true
- fast algorithm for drawing filled circles?
- Why use the Bitwise-Shift operator for values in a C enum definition?