User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:stdio.h:fprintf

fprintf

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

Converts (according to format format) and writes output to stream stream. Number of characters written, or negative value on error, is returned.

optional flag:

- left adjust
+ always sign
0 zero pad
# Alternate form: for conversion character o, first digit will be zero, for [xX], prefix 0x or 0X to non-zero value, for [eEfgG], always decimal point, for [gG] trailing zeros not removed.

optional minimum width: if specified as *, value taken from next argument (which must be int).
optional . (separating width from precision)
optional precision: for conversion character s, maximum characters to be printed from the string, for [eEf], digits after decimal point, for [gG], significant digits, for an integer, minimum number of digits to be printed. If specified as *, value taken from next argument (which must be int).

optional length modifier:

h short or unsigned short
l long or unsigned long
L long double

conversion character:

d,i int argument, printed in signed decimal notation
o int argument, printed in unsigned octal notation
x,X int argument, printed in unsigned hexadecimal notation
u int argument, printed in unsigned decimal notation
c int argument, printed as single character
s char* argument
f double argument, printed with format [-]mmm.ddd
e,E double argument, printed with format [-]m.dddddd(e|E)(+|-)xx
g,G double argument
p void* argument, printed as pointer
n int* argument : the number of characters written to this point is written into argument
% no argument; prints %

C Sourcecode Example

/* 
 * fprintf example code
 * http://code-reference.com/c/stdio.h/fprintf 
 */
 
 
#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
#define MAX 60
 
int main ( void )
{
   FILE * stream;
   char string[MAX];
   char c;
 
   sprintf(string,"This string will be cut here - This will not be shown");
 
   stream = fopen ("test.txt","w");
   fprintf (stream, "%-8.28s ",string); // -8 is minimun and after the . (in this case 28) is maximum chars
 
   freopen("test.txt", "r", stream);
   while((c = fgetc(stream) ) != EOF) {
      printf("%c", c );
      }
 
   printf("\n");
   fclose (stream);
   return 0;
}

fprintf output example

  output:./fprintf 
  This string will be cut here 

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/stdio.h/fprintf.txt · Last modified: 2024/02/16 01:05 (external edit)

Impressum Datenschutz