Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C stdio fprintf() Function

❮ C stdio Library


Example

Write into a file:

FILE *fptr;

// Open a file in writing mode
fptr = fopen("filename.txt", "w");

// Write some text to the file
fprintf(fptr, "Some text");

// Close the file
fclose(fptr);
Try it Yourself »

Definition and Usage

The fprintf() function writes a formatted string into a file.

The fprintf() function is defined in the <stdio.h> header file.

Format specifiers

The format string can contain format specifiers which describe where and how to represent additional arguments that are passed into the function.

The format specifiers have the form %[flags][width][.precision][length]specifier. The components in [square brackets] are optional.

An explanation of each of the components:

  • flags - Optional. A sequence of any of the following characters:
    • - - Makes the output left-justified by adding any padding spaces to the right instead of to the left.
    • # - Shows an alternate representation of the formatted data depending on the conversion.
    • + - Causes positive numbers to always be prefixed with "+".
    • - (A space character) This prefixes a space to positive numbers, primarily so that the digits can be lined up with the digits of negative numbers.
    • 0 - Pads numbers with zeroes on the left.
  • width - Optional. A whole number specifying the minimum number of characters that the output should occupy. If necessary, spaces are added to the right to reach this number, or to the left if the - flag is used. If an *asterisk is used then the width is given by the argument preceding the one being represented.
  • .precision - Optional. A . followed by a whole number indicating how many decimal digits to show in the formatted data.
  • length - Optional. A sequence of characters which changes the expected data type of the argument. It can be one of the following:
    • hh - Expect char type for whole numbers.
    • h - Expect short int type for whole numbers.
    • l - Expect long int type for whole numbers.
             Expect wint_t type for characters.
             Expect wchar_t* type for strings.
    • ll - Expect long long int type for whole numbers.
    • j - Expect intmax_tor uintmax_t type for whole numbers.
    • z - Expect size_t type for whole numbers.
    • t - Expect ptrdiff_t type for whole numbers.
    • L - Expect long double type for floating point numbers.
  • specifier - Required. A character which indicates how an argument's data should be represented. The list of possible characters is shown in the table below.

List of specifiers

Character Specifier Description
d or i Decimal integer Represents a whole number as a decimal integer.
u Unsigned decimal integer Represents a whole number as an unsigned decimal integer.
o Octal integer Represents a whole number as an octal integer. The "#" flag will prefix the number with "0".
x or X Hexadecimal integer Represents a whole number as a hexadecimal integer. The "#" flag will prefix the number with "0x". If "X" is used then digits A to F and the letter X are shown in uppercase.
f or F Floating point number Represents a floating point number. If "F" is used then letters (from values like "nan") will be represented in uppercase. The "#" flag will force a decimal point even if there are no decimal digits.
e or E Scientific notation Represents a floating point number in scientific notation. If "E" is used then letters will be represented in uppercase. The "#" flag will force a decimal point even if there are no decimal digits.
g or G General number Uses the shortest representation between f and e for a floating point number. If "G" is used then it chooses between F and E instead.
a or A Hexadecimal floating point number Writes a floating point number's internal representation with hexadecimal digits. If "A" is used then the digits are represented in uppercase.
c Character Represents a character. If the argument is an integer then it represents the character for the ASCII value specified by the integer.
s String Represents a string.
p Pointer Represents the memory address of a pointer, usually with hexadecimal digits.
n No output The number of characters that have been written to the file up to this point is written into the argument. The argument must be a pointer to an integer.
% Percent symbol Represents a literal "%" character.

A variety of examples of how to use format specifiers can be found in the printf() function reference page.


Syntax

fprintf(FILE * fptr, const char * format, arg1, arg2...);

Parameter Values

Parameter Description
fptr Required. A file pointer, usually created by the fopen() function.
format Required. A string representing the format of the data to be written to the file.
arg1, arg2... Optional. Any number of additional arguments, their values can be formatted and written to the file through the specifiers in the format argument.

Technical Details

Returns: An int value representing the number of characters written to the file. If an error occurred then it returns a negative number.

❮ C stdio Library

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.