Function fputc () in C

The function int fputc (int char, FILE * stream) in Library C writes one character (an unsigned char) defined by the char parameter to the given Stream and increases the position indicator for the Stream.

The function int fputc (int char, FILE * stream) in Library C writes one character (an unsigned char) defined by the char parameter to the given Stream and increases the position indicator for the Stream.

Declare function fputc () in C

Below is the declaration for fputc () in C:

 int  fputc ( int char ,  FILE  * stream ) 

Parameters

char - This is the character written.

stream - This is the pointer to a FILE object that identifies the Stream, where the characters are recorded.

Returns the value

If there is no error, the same character will be returned. If an error occurs, the EOF is returned and the Error Indicator is set.

For example

The following program C illustrates the usage of fputc () in C:

 #include int  main  () {  FILE  * fp ; int  ch ;  fp  =  fopen ( "baitapc.txt" , "w+" ); for (  ch  = 33 ;  ch  <= 100 ;  ch ++ ) {  fputc ( ch ,  fp ); }  fclose ( fp ); return ( 0 ); } 

Compiling and running the above program will create a baitc.t.txt in the current directory, and will have the following content:

  ! "# $% & '() * +, -. / 0123456789:; <=>? @ ABCDEFGHIJKLMNOPQRSTUVWXYZ [] ^ _` abcd 

Now follow the contents of the above file by using the following C program:

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

Compiling and running the above program will result:

Picture 1 of Function fputc () in C

According to Tutorialspoint

Previous lesson: Function fgets () in C

Next lesson: Function fputs () in C

You've just finished reading the article "Function fputc () in C" edited by the TipsMake team. You can save function-fputc-in-c.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.

« PREV : Function fputs () in...
Function fgets () in... : NEXT »