Printf width specifier to maintain precision of floating-point value

I recommend @Jens Gustedt hexadecimal solution: use %a. OP wants “print with maximum precision (or at least to the most significant decimal)”. A simple example would be to print one seventh as in: But let’s dig deeper … Mathematically, the answer is “0.142857 142857 142857 …”, but we are using finite precision floating point numbers. … Read more

Using floats with sprintf() in embedded C

Since you’re on an embedded platform, it’s quite possible that you don’t have the full range of capabilities from the printf()-style functions. Assuming you have floats at all (still not necessarily a given for embedded stuff), you can emulate it with something like: You’ll need to restrict how many characters come after the decimal based … Read more

How do you allow spaces to be entered using scanf?

People (and especially beginners) should never use scanf(“%s”) or gets() or any other functions that do not have buffer overflow protection, unless you know for certain that the input will always be of a specific format (and perhaps not even then). Remember than scanf stands for “scan formatted” and there’s precious little less formatted than user-entered data. It’s ideal if you have total control of … Read more

Print a boolean value with printf

I assume you are running into a buffering issue, such that your program exits before your buffer flushes. When you use printf() or print() it doesn’t necessarily flush without a newline. You can use an explicit flush() or add a new-line (which will also cause a flush()) See also Buffered Streams – The Java Tutorials, Flushing Buffered Streams which says in part Some buffered … Read more