fputc in c

If you want to know more on fputc in c then read this blog post.

What is fputc in c programming?

fputc function writes single character at a time to a given file.

This function uses file pointer to write into a file.

This function returns the characters written in a file or EOF on error.

fputc is defined in stdio.h header file.

This function writes the data character by character to a given file.

This function terminates the process when it encounters EOF in a file.

Prototype

#include<stdio.h>
int fputc(int char,FILE *stream);

Parameters of fputc in c

fputc function takes argument char and a file pointer.

Return value of fputc in c

fputc returns the characters written in a file or EOF on error.


Example program for fputc in c

#include<stdio.h>
int main()
{
  FILE *fpointer;
  int i;
  char text[30]="scholarsoul";
  fpointer=fopen("scholar.txt","w");
  if(fpointer== NULL)
   return 0;
  for(i=0;i<text[i]!='\0';i++)
   fputc(text[i],fpointer);
  fclose(fpointer);
  return 0;
}
Example for fputc in c
Example for fputc in c
Example fputc in c output
Example fputc in c output

Code explanation

The given input string is written in to the file by calling fputc function.

fputc() Man page


Difference between fputc and fgetc in c

fputc function writes a single character at a time to a given file. fgetc is a function that is used to obtain input from a file.

fputc function returns the characters written in a file or EOF on error. fgetc returns the characters read from a file.

fputc takes two arguments. fgetc takes one argument.

Prototypes are as follows

int fputc(int char,FILE *fpointer);

int fgetc(FILE *fpointer);
fputc in cfgetc in c
Used to write single character at a time to a file.Used to obtain input from a file.
int fputc(int char,FILE *fpointer);int fgetc(FILE *fpointer);
Returns character written in a file otherwise EOF as error.Returns the characters read from a file.

Difference between fputc and fputs in c

fputc function writes single character at a time to a given file. fputs writes a string to the specified stream.

fputc function returns the characters written in a file or EOF on error. fputs returns a non-negative value or EOF on error.

Prototypes are as follows

int fputc(int char,FILE *fpointer);

int fputs(const char *string,FILE *stream);
fputc in cfputs in c
Used to write single character at a time to a file.Writes a string to the specified stream.
int fputc(int char,FILE *fpointer);int fputs(const char *string,FILE *stream);
Returns character written in a file otherwise EOF as error.Returns a non-negative value or EOF on error.

Difference between fputc and putc in c

fputc function writes a single character at a time to a given file. putc function writes a character to the specified stream.

fputc function returns the characters written in a file or EOF on error. putc returns the character written as an unsigned char cast to an int or error.

fputc works slower than putc.

fputc takes less space than putc.

Prototypes are as follows

int fputc(int char,FILE *fpointer);

int putc(int char,FILE *strm);
fputc in cputc in c
Writes single character at a time to a given file.Writes a character to the specified stream.
int fputc(int char,FILE *fpointer);int putc(int char,FILE *strm);
Returns the characters written in a file or EOF on error.Returns the character written as an unsigned char cast to an int or error.

Difference between fputc and puts in c

fputc function writes a single character at a time to a given file. puts function writes a string to stdout up to but not including null character.

fputc function returns the characters written in a file or EOF on error. puts return non-negative value or EOF on error.

fputc takes two arguments. puts takes one argument.

fputc uses pointer for seek operation in a file. puts uses pointer to write string value on stdout.

Prototypes are as follows

int fputc(int char,FILE *fpointer);

int puts(const char *string);
fputc in cputs in c
Writes single character at a time to a given file.Writes a string to stdout upto but not including null character.
int fputc(int char,FILE *fpointer);int puts(const char *string);
Returns the characters written in a file or EOF on error.Returns non-negative value or EOF on error.

People also ask for

What is the difference between fputc and fprintf in c?

fputc writes a single character at a time to a given file. fprintf sends formatted output to a stream.

What is the difference between fputc and printf in c?

fputc writes a single character at a time to a given file. printf prints the data onto the output screen.

What is the difference between fputc and putchar in c?

fputc writes a single character at a time to a given file. putchar function writes a single character to the output stream.

What does fputc stands for?

fputc stands for put a byte on a stream. It is defined in stdio.h header file.

What are all the errors fputc returns?

fputc returns following errors
1.EAGAIN
2.EBADF
3.EFBIG
4.EINTR
5.EIO
6.ENOSPC
7.EPIPE