In C++, does the instruction printf("%4.2f", num);
display four digits including two decimal places?

- 555,201
- 31
- 458
- 770

- 1
- 1
- 1
-
3Can't you just make a small program to test this? – anastaciu May 26 '20 at 21:07
-
2What does your class-notes say? The tutorials you have read? The text-books you have? The [references](https://en.cppreference.com/w/c/io/fprintf) available? Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also take the [tour] and read [ask]. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 26 '20 at 21:08
-
You could have easily read about the printf and format specifier. It isn't hard to find from already existing sources, and there are good ones. – Abhishek Bhagate May 26 '20 at 21:10
-
write a test one liner (ideone.com if you dont have a compiler) and update the question with the answer – pm100 May 26 '20 at 21:11
-
By the way, please don't use the old C function `printf` in C++. It's not type-safe and you can easily enter the realm of *undefined behavior* way to easily. If you don't have any decent books, then [here's a list of good ones](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), I really recommend you invest in a few. – Some programmer dude May 26 '20 at 21:14
-
2@Someprogrammerdude always sad when I see a reference to a really useful question that would now not be permitted – pm100 May 26 '20 at 21:15
-
@pm100 Life was easier before we all got so strict. :) On the other hand, a community as big as this with so many new users would fall into chaos immediately without some strictness. – Some programmer dude May 26 '20 at 21:36
2 Answers
The instruction
printf("%4.2f", num);
displays four digits including two decimal places?
No, that is not how it works.
A format string is specified in the following format:
%[flags][width][.precision][size]type
Specifiers in []
are optional.
The width specifier indicates the minimum number of characters to output total (it may be more!). If not specified, the width defaults to 1.
The precision specifier indicates how many digits the f
type prints after a decimal point (the precision has other meanings for different types). If not specified, the precision defaults to 6 for the f
type.
In your example format string: %4.2f
, 4
is the width, 2
is the precision, and f
is the type.
So, for example, given double num = 123.0;
:
printf("%f", num);
Has a width of 1 and a precision of 6, so it prints "123.000000"
- at least 1 character, including the decimal and 6 digits following the decimal.
printf("%4.2f", num);
Has a width of 4 and a precision of 2, so it prints "123.00"
- at least 4 characters, including the decimal and 2 digits following the decimal.
printf("%10.2f", num);
Has a width of 10 and a precision of 2, so it prints " 123.00"
- at least 10 characters, including 4 leading spaces added as padding, the decimal, and 2 digits following the decimal.
Refer to this printf
reference for more details about the format string and its inputs and outputs.

- 555,201
- 31
- 458
- 770
-
1incorrect the width specifies the min width of the field, it is padded with spaces if needed https://ideone.com/rLgO1A – pm100 May 26 '20 at 21:24
-
1
-
friend I'm poor in programming, just tell me if it's true or false – KaliIsBetter May 26 '20 at 22:01
-
@Eindid It is false. How do the examples in my answer, particularly the 2nd example, not address that question for you? – Remy Lebeau May 26 '20 at 22:11
The 4 is the width of the field and the 2 after the point is the precision specification for the float that you want to print. But it also depends on what is the value of num. So, no it is not guaranteed to be 4 digits. If num is 234342.43, then it is not going to be 4 digits.

- 734
- 5
- 10
-
friend I'm poor in programming, just tell me if it's true or false – KaliIsBetter May 26 '20 at 22:01
-
@Eindid I have suggested an example that it is not going to be four digits. So the answer is false. In any case, you could have written a program involving just a few lines to see this for yourself. – stackoverblown May 26 '20 at 23:35