fprintf

Raheelanjum
3 min readDec 9, 2024

The fprintf function allows you to "write" information to the screen for the user to view. This is very important when user interaction is involved. The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read.

The fprintf function

The fprintf function is used for printing information to the screen. The fprintf function prints an array of characters to the screen:

fprintf('HaBirthday\n'););

We often use the fprintf statement to show the user information stored in our variables. To "place" a number into this string of printed characters, we use several formatting options, but they all start with a % sign. It should be noted that when used in this way, the % is not the comment character.

Below are several examples of printing information from variables using fprintf. Notice the use of %s to print a string, %d to print an integer, and %f to print a number with a decimal (a floating point number). The value of each variable is "plugged into" the appropriate format string at the appropriate location as marked by the % sign in the string.

fprintf('My name is %s\prof_name);

fprintf('My('My age is %d and my salary is $%.2f\n', prof_aprof_salary););

--

--

Raheelanjum
Raheelanjum

Written by Raheelanjum

Highly skilled and experienced content writer dedicated to crafting compelling, informative content that resonates with diverse audiences. Expertise spans blog

Responses (10)