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

C library function - fprintf()? ›

fprintf() -- Write Formatted Data to a Stream

What library is fprintf in C? ›

C has a library function fprintf, also known as the format print function, that sends formatted output string to a stream. The fprintf in C is used to write data in a file using a stream object, not in the stdout console.

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, ...])

Is printf () a library function? ›

printf is a C function belonging to the ANSI C standard library, and included in the file stdio. h. Its purpose is to print formatted text to the standard output stream. Hence the "f" in the name stands for "formatted".

What is the difference between the printf () and fprintf () functions? ›

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.

Where are library functions in C? ›

All C standard library functions are declared by using many header files. These library functions are created at the time of designing the compilers. All the header files should have a ". h" extension.

What is the library for print in C? ›

In C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). The printf function is a part of the C standard library <stdio. h> and it can allow formatting the output in numerous ways.

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.

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 .

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

Difference between fwrite() and fprintf()?

So if you pass a pointer to an integer to fwrite , it writes the bytes of the integer into the file just as they are represented in RAM. fprintf prints according to a formatting string where you can specify how the data is supposed to be formatted.

How to use printf function in C? ›

Basic Printf Examples

Displaying a simple message: printf("Hello, world!"); This example displays a simple message. The output will be: "Hello, world!" 2. Displaying an integer: int number = 42; printf("The answer is: %d", number);

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

The two PRINT procedures perform formatted output. PRINT performs output to the standard output stream (IDL file unit -1), while PRINTF requires a file unit to be explicitly specified. Note: IDL uses the standard I/O function sprintf to do its formatting.

Is scanf () a library function? ›

h header file is also part of the standard C library and contains declarations for input and output functions like scanf() , printf() , and others. To use the scanf() function in a C program, you need to include the stdio. h header file at the beginning of your program using the #include preprocessor directive.

What is fprintf() in C? ›

The function fprintf() is known as format print function. It writes and formats the output to a stream. It is used to print the message but not on stdout console. Here is the syntax of fprintf() in C language, int fprintf(FILE *fptr, const char *str, ... );

Is fprintf better than printf? ›

printf() is perfect for fast and simple console printing, while fprintf() enables file output and robust error handling.

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 library is Sprintf in C? ›

One commonly used function in the standard C library is sprintf() , which is used to format a string with variables. This also includes variations like snprintf() and also functions that call snprintf() internally, like Log.info() and String::format() .

In which library gets is used in C? ›

gets is a function in the C standard library, declared in the header file stdio. h , that reads a line from the standard input and stores it in a buffer provided by the caller.

What library is system in C? ›

The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed. <stdlib.

How to find libraries in C? ›

Libaries consist of a set of related functions to perform a common task; for example, the standard C library, 'libc. a', is automatically linked into your programs by the “gcc” compiler and can be found at /usr/lib/libc. a. Standard system libraries are usually found in /lib and /usr/lib/ directories.

Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated:

Views: 5884

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.