C library function - fprintf() (2024)

C library function - fprintf() (1)

  • The C Standard Library
  • C Library - Home
  • C Library - <assert.h>
  • C Library - <ctype.h>
  • C Library - <errno.h>
  • C Library - <float.h>
  • C Library - <limits.h>
  • C Library - <locale.h>
  • C Library - <math.h>
  • C Library - <setjmp.h>
  • C Library - <signal.h>
  • C Library - <stdarg.h>
  • C Library - <stddef.h>
  • C Library - <stdio.h>
  • C Library - <stdlib.h>
  • C Library - <string.h>
  • C Library - <time.h>
  • C Standard Library Resources
  • C Library - Quick Guide
  • C Library - Useful Resources
  • C Library - Discussion
  • C Programming Resources
  • C Programming - Tutorial
  • C - Useful Resources
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

Description

The C library function int fprintf(FILE *stream, const char *format, ...) sends formatted output to a stream.

Declaration

Following is the declaration for fprintf() function.

int fprintf(FILE *stream, const char *format, ...)

Parameters

  • stream − This is the pointer to a FILE object that identifies the stream.

  • format − This is the C string that contains the text to be written to the stream. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. Format tags prototype is %[flags][width][.precision][length]specifier, which is explained below −

Sr.No.specifier & Output
1

c

Character

2

d or i

Signed decimal integer

3

e

Scientific notation (mantissa/exponent) using e character

4

E

Scientific notation (mantissa/exponent) using E character

5

f

Decimal floating point

6

g

Uses the shorter of %e or %f

7

G

Uses the shorter of %E or %f

8

o

Signed octal

9

s

String of characters

10

u

Unsigned decimal integer

11

x

Unsigned hexadecimal integer

12

X

Unsigned hexadecimal integer (capital letters)

13

p

Pointer address

14

n

Nothing printed

15

%

Character

Sr.No.Flags & Description
1

-

Left-justifies within the given field width; Right justification is the default (see width sub-specifier).

2

+

Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a -ve sign.

3

(space)

If no sign is written, a blank space is inserted before the value.

4

#

Used with o, x or X specifiers. The value is preceded with 0, 0x or 0X respectively for values different than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow then no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.

5

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).

Sr.No.Width & Description
1

(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

2

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Sr.No..precision & Description
1

.number

For integer specifiers (d, i, o, u, x, X) − precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E and f specifiers: this is the number of digits to be printed after the decimal point. For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type: it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

2

.*

The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Sr.No.Length & Description
1

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

2

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

3

L

The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).

  • additional arguments − Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter, if any. There should be the same number of these arguments as the number of %-tags that expect a value.

Return Value

If successful, the total number of characters written is returned otherwise, a negative number is returned.

Example

The following example shows the usage of fprintf() function.

#include <stdio.h>#include <stdlib.h>int main () { FILE * fp; fp = fopen ("file.txt", "w+"); fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012); fclose(fp); return(0);}

Let us compile and run the above program that will create a file file.txt with the following content −

We are in 2012

Now let's see the content of the above file using the following program −

#include <stdio.h>int main () { FILE *fp; int c; fp = fopen("file.txt","r"); while(1) { c = fgetc(fp); if( feof(fp) ) { break; } printf("%c", c); } fclose(fp); return(0);}

Let us compile and run above program to produce the following result.

We are in 2012

stdio_h.htm

Advertisem*nts

';adpushup.triggerAd(ad_id); });

C library function - fprintf() (2024)

FAQs

What is fprintf() in C? ›

In the C programming language, fprintf() sends formatted output to a file stream. The fprintf() function helps print content in a file instead of on the stdout console. The definition of the fprintf() function is included in stdio. h header file.

How do printf () and fprintf () functions compare? ›

printf places output on the standard output stream stdout. fprintf places output on the named output stream. sprintf places "output", followed by the null character (\0) in consecutive bytes starting at * s; it is the user's responsibility to ensure that enough storage is available.

What does the fprintf function do? ›

The fprintf function allows you to "write" information to the screen for the user to view. This 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.

Does fprintf write to a file? ›

fprintf( fileID , formatSpec , A1,...,An ) applies the formatSpec to all elements of arrays A1,... An in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen .

What is the similar function to fprintf? ›

The fprintf() function is similar to printf() , but writes its output to the stream specified by fp rather than to stdout .

When would you use fprintf instead of disp function? ›

Typically use fprintf() for all output statements in your program that produce problem specific output. Typically use disp() only when you are debugging for "quick and dirty" output to help find errors. When you are not sure what precision a computed value will have, display it with the %g format descriptor.

What is the difference between write and fprintf in C? ›

fwrite prints in byte format which is unreadable when printed while fprintf prints in text format I.e strings which can be read.

Does fprintf print to stdout? ›

The fprintf(stdout, ) function sends its output to a logical file commonly known as the standard output or simply stdout. When a program is run in the Unix environment, the logical file stdout is by default associated with the screen being viewed by the person who started the program.

What is the difference between print () and printf ()? ›

PRINT performs output to the standard output stream (IDL file unit -1), while PRINTF requires a file unit to be explicitly specified.

Can you use the fprintf() to display the output on the screen? ›

Yes, you can. fprintf() syntax: int fprintf(FILE *stream, const char *format, parameters); Here stream can be specified to standard output by specifying stdout.

What is the return value of fprintf? ›

Return Value

The fprintf() function returns the number of bytes that are printed or a negative value if an output error occurs. For information about errno values for fprintf() , see printf() — Print Formatted Characters.

What is the difference between fprintf and fscanf in C? ›

In the fprintf() function, the file pointer points to the file where the formatted output will be written, and if the write is successful, it will print the total count of characters. In comparison, the purpose of the fscanf() function is to read formatted data from a file.

How to use fprintf() in C? ›

Writing File : fprintf() function

The fprintf() function is used to write set of characters into file. It sends formatted output to a stream. Syntax: int fprintf(FILE *stream, const char *format [, argument, ...])

What is the difference between printf and fprintf? ›

The difference between printf and fprintf is that printf is used to print a formatted string to a standard output which is most of the time a computer screen and fprintf is used to print a formatted string to a specific file.

What are the arguments for fprintf? ›

The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-defined manner. The argument shall be a pointer to an integer into which is written the number of bytes written to the output so far by this call to one of the fprintf() functions.

What does %f print in C? ›

The %f is the floating point format specifier in C language that can be used inside the formatted string for input and output of float data type. Apart from %f, we can use %e or %E format specifiers to print the floating point value in the exponential form.

What is fscanf() in C? ›

The fscanf() function reads data from the current position of the specified stream into the locations that are given by the entries in argument-list, if any. Each entry in argument-list must be a pointer to a variable with a type that corresponds to a type specifier in format-string.

How to use print function in C? ›

The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement.

References

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6686

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.