Padding characters in printf

Pure Bash, no external utilities This demonstration does full justification, but you can just omit subtracting the length of the second string if you want ragged-right lines. pad=$(printf ‘%0.1s’ “-“{1..60}) padlength=40 string2=’bbbbbbb’ for string1 in a aa aaaa aaaaaaaa do printf ‘%s’ “$string1” printf ‘%*.*s’ 0 $((padlength – ${#string1} – ${#string2} )) “$pad” printf ‘%s\n’ … Read more

printf() formatting for hexadecimal

The # part gives you a 0x in the output string. The 0 and the x count against your “8” characters listed in the 08 part. You need to ask for 10 characters if you want it to be the same. int i = 7; printf(“%#010x\n”, i); // gives 0x00000007 printf(“0x%08x\n”, i); // gives 0x00000007 … Read more

C dynamically printf double, no loss of precision and no trailing zeroes

There’s probably no easier way. It’s a quite involved problem. Your code isn’t solving it right for several reasons: Most practical implementations of floating-point arithmetic aren’t decimal, they are binary. So, when you multiply a floating-point number by 10 or divide it by 10, you may lose precision (this depends on the number). Even though … Read more

Turbo C++: Why does printf print expected values, when no variables are passed to it?

The code has undefined behaviour. In Turbo C++, it just so happens that the three variables live at the exact positions on the stack where the missing printf() argument would be. This results in the undefined behaviour manifesting itself by having the “correct” values printed. However, you can’t reasonably rely on this to be the … Read more

Printf without newline in assembly

fflush() flushes buffered output in line or full-buffered stdio streams: extern fflush … xor edi, edi ; RDI = 0 call fflush ; fflush(NULL) flushes all streams … Alternatively, mov rdi, [stdout] / call fflush also works to flush only that stream. (Use default rel for efficient RIP-relative addressing, and you’ll need extern stdout as … Read more