C Function Fprintf
[ C Standard Library - ](#)
## Description
The C library function **int fprintf(FILE *stream, const char *format, ...)** sends formatted output to the stream `stream`.
## Declaration
The following is the declaration for the `fprintf()` function.
int fprintf(FILE *stream, const char *format, ...)
## Parameters
* **stream** -- This is a pointer to a `FILE` object that identifies the stream.
* **format** -- This is a C string that contains the text to be written to the stream `stream`. It can contain embedded format tags, which are replaced by the values specified in subsequent additional arguments and formatted accordingly. The format tag syntax is **%[.precision]specifier**, explained as follows:
| specifier | Output |
| --- | --- |
| c | Character |
| d or i | Signed decimal integer |
| e | Scientific notation (mantissa and exponent) using the letter `e` |
| E | Scientific notation (mantissa and exponent) using the letter `E` |
| f | Decimal floating point |
| g | Uses the shorter of `%e` or `%f` |
| G | Uses the shorter of `%E` or `%f` |
| o | Signed octal |
| s | String of characters |
| u | Unsigned decimal integer |
| x | Unsigned hexadecimal integer |
| X | Unsigned hexadecimal integer (uppercase letters) |
| p | Pointer address |
| n | No output |
| % | Percent sign |
| flags | Description |
| --- | --- |
| - | Left-align within the given field width; right alignment is the default (see the `width` sub-specifier). |
| + | Forces the result to be preceded by a plus or minus sign (`+` or `-`) β i.e., positive numbers will display a `+` sign. By default, only negative numbers are preceded by a `-` sign. |
| (space) | If no sign is written, a blank space is inserted before the value. |
| # | Used with `o`, `x`, or `X`: non-zero values are preceded by `0`, `0x`, or `0X`, respectively. Used with `e`, `E`, and `f`: forces the output to contain a decimal point even when no digits follow it. By default, if no digits follow, no decimal point is displayed. Used with `g` or `G`: same effect as with `e` or `E`, but trailing zeros are not removed. |
| 0 | Pads numbers with leading zeros (0) instead of spaces (see the `width` sub-specifier). |
| width | Description |
| --- | --- |
| (number) | Minimum number of characters to output. If the value to be printed is shorter than this number, the result is padded with spaces. If the value is longer, it is not truncated. |
| * | Width is not specified in the format string but is provided as an additional integer argument before the argument to be formatted. |
| .precision | Description |
| --- | --- |
| .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, it is padded with leading zeros. If the value is longer, it is not truncated. A precision of `0` means no characters are written. For `e`, `E`, and `f`: number of digits to be printed after the decimal point. For `g` and `G`: maximum number of significant digits to be printed. For `s`: maximum number of characters to be printed. By default, all characters are printed until the terminating null character is encountered. For `c`: has no effect. If no precision is specified, the default is `1`. If `.` is specified without an explicit value, the precision is assumed to be `0`. |
| .* | Precision is not specified in the format string but is provided as an additional integer argument before the argument to be formatted. |
| length | Description |
| --- | --- |
| h | The argument is interpreted as a short int or unsigned short int (only applicable to integer specifiers: `i`, `d`, `o`, `u`, `x`, and `X`). |
| l | The argument is interpreted as a long int or unsigned long int, applicable to integer specifiers (`i`, `d`, `o`, `u`, `x`, and `X`) and specifiers `c` (for a wide character) and `s` (for a wide character string). |
| L | The argument is interpreted as a long double (only applicable to floating-point specifiers: `e`, `E`, `f`, `g`, and `G`). |
* **Additional arguments** -- Depending on the `format` string, the function may require a series of additional arguments, each containing a value to be inserted, replacing each `%` tag specified in the `format` parameter. The number of arguments should match the number of `%` tags.
## Return Value
On success, the function returns the total number of characters written; otherwise, it returns a negative number.
## Example
The following example demonstrates the usage of the `fprintf()` function.
## Example
#include
#include
int main()
{
FILE * fp;
fp =fopen("file.txt","w+");
fprintf(fp,"%s %s %s %d","We","are","in",2014);
fclose(fp);
return(0);
}
Let us compile and run the above program; it will create the file **file.txt**, whose content is as follows:
We are in 2014
Now let us use the following program to view the contents of the above file:
## Example
#include
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);
}
[ C Standard Library - ](#)
YouTip