Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ streamsize prec = cout.precision(3) - How does it work?

I am kind of newbie in using c++. I have a quick question, probably a dumb question.

streamsize prec = cout.precision(3);

As I understand correctly this declaration works like that: set the cout precision to 3, but assign the previous precision value to prec.

Also, simply, we can assign a function result (say a math addition function) to a variable:

int z = addition(3,4);

In the second one, it does the calculation and assigns the results to the variable z, not the previous value or a default value. Is my understanding correct? What is the difference between them?

like image 492
user2371160 Avatar asked Oct 22 '22 10:10

user2371160


1 Answers

What value a function returns depends entirely on that particular function. Most functions simply return a result of their operation.

The state-setting functions in standard library streams (such as precision) are a bit unusual in their interface of "I set a new value and return the old one," but it's still perfectly valid, as long as the function's behaviour is documented (which it is in their case).

like image 189
Angew is no longer proud of SO Avatar answered Nov 12 '22 22:11

Angew is no longer proud of SO