meaning of f in fprintf function

So this is just a general question, as I familiarize myself with the fprintf and all other glorious functions...
When I use the /f or form feed as it's called, what's that actually doing? What does 'form feed' mean?
Also does anyone know why it's 'fprintf' and not just 'print'?
Thanks in advance!

 Accepted Answer

Chunru
Chunru on 20 Mar 2024
In old days, the command \f is used to make a printer to move to beginning of next pages.
fprintf or print or printf are used by different programming languages for the similar (but may be different) purpose. The begining "f" means file print. The ending "f" means the formatted print.

8 Comments

Thank you. Can you also clear up what 'form feed' means in relation to fprintf? I see it's normally used as well as /i for 'integer' when I am dealing with it, but I don't know the discernable differences. Why would I ever need to use /f instead of say, /i?
See my simple code below to understand why I am asking.
grade=rand(1,49)*(100-20)+20;
grade_avg=mean(grade);
fprintf('The average grade is %1.0f \n',grade_avg) % is /f the best to use here?
if grade_avg>=60
disp('passing average')
else
disp('failing average')
end
You can read more about this here. Note that %f, \f, and %i all have different meaning.
I find the simplest way to know is to test them out.
A = pi*ones(1,3);
fprintf('Displaying pi: \n\f %f \n %.2f \n %12f', A)
Displaying pi: □ 3.141593 3.14 3.141593
It would appear that, on a monitor, \f has no visual effect.
Generally, "\" followed by a character means special/control characters. For example, \n, \f, \n
'%" followed by other characters specifies format. Eg. '%f' (float point) '%d' or "%i" integers, "%c" for character, "%s" for strings.
I see. Thank you very much. fprintf is one of those complex functions with many ways to edit it to fit ones needs but in the beginning it is quite daunting to use.
"I see it's normally used as well as /i for 'integer' when I am dealing with it, but I don't know the discernable differences."
No, you are mixing things up:
  • %i is the formatting operator for converting an integer to text.
  • \f is the representation of the special character named "Form Feed".
Two completely different things.
"Why would I ever need to use /f instead of say, /i?"
The forward-slash has no special meaning, it is interpreted literally. So the actual answer to your question is "whenever you want a literal forward-slash and f or i":
fprintf('/f /i', pi, int8(3))
/f /i
You are confusing several different features of the format specifier, so I will presume that you intended to ask about the formatting operators %f and %i. They are commonly used to convert floating point and integer values respectively:
fprintf('%f %i', pi, int8(3))
3.141593 3
Note how the values are formatted when they are swapped around (the integer is internally coerced into a floating point value, whereas PI() forces FPRINTF to use the default %e):
fprintf('%f %i', int8(3), pi)
3.000000 3.141593e+00
I see. I wonder why pi is a special case here. I suppose it's all nuance. It's answering another question of why I would chose to use %f over %i in cases like this where pi is a part of my fprintf function. Thank you.
Stephen23
Stephen23 on 20 Mar 2024
Edited: Stephen23 on 20 Mar 2024
"I wonder why pi is a special case here"
PI is not a special case here.
PI is just a convenient binary floating point value that I used to demonstrate the effect of different formatting operators with. Forget about PI, you can pick any other binary floating point values with fractional part.
What would be another example of another binary floating point value? i^i or 0.20788?

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023b

Tags

Asked:

on 20 Mar 2024

Commented:

on 21 Mar 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!