Home » C programs » stdio.h header file functions

fputc() function in C language with Example

Here, we are going to learn about the fputc() function of library header stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on January 09, 2019

fputc() function in C

Prototype:

    int fputc(const char ch, FILE *filename);

Parameters:

    const char ch, FILE *filename

Return type: int

Use of function:

In the file handling, through the fputc() function we take the next character from the input stream buffer and put the characters in the file and increments the file pointer by one. The prototype of the function fputc() is: int fputc(const char ch, FILE *filename);

Here it put the characters into the specified files and filename is the name of file stream.

fputc() example in C

#include <stdio.h>
#include <stdlib.h>

int main()
{
	//Initialize the file pointer
	FILE *f;
	char ch;
	
	//Create the file for write operation
	f=fopen("includehelp.txt","w");
	
	printf("Enter five character\n");
	for(int i=0;i<5;i++){
		//take the characters from the users
		scanf("%c",&ch);
		//write back to the file
		fputc(ch,f);
		//clear the stdin stream buffer
		fflush(stdin);
	}		
	//close the file after write operation is over
	fclose(f);
	
	//open a file
	f=fopen("includehelp.txt","r");
	
	printf("\n...............print the characters..............\n\n");
	while(!feof(f)){
		//takes the characters in the character array 
		ch=fgetc(f);
		//and print the characters
		printf("%c\n",ch);
	}
	fclose(f);
	
	return 0;
}

Output

fputc example in c






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.