Home » C programs » stdio.h header file functions

putc() function of stdio.h in C



In this article, we are going to learn about the putc() function of stdio.h header file in C programming language, and use it to put characters inside a file with the help of file pointer.
Submitted by Abhishek Sharma, on April 12, 2018

If you are handling files in your application then coping one file into other or from the same file will something you will encounter. Therefore, using the function putc() will help you doing this.

The Function is use to write a single character in file. The Function needs only two parameters first a character and second is the file itself.

Example:

    putc('a', FILE);
    //Here, FILE is the file variable.
    
    Output:
    It will write 'a' on the file at the current 
    position of the cursor.

stdio.h - putc() function Example in C

#include <stdio.h>

int main ()
{
	// initializing the type of variables
	FILE *F;
	int c;

	// opening the file in write mode
	F = fopen("abc.txt", "w");
	for( c = 33 ; c <= 100; c++ ) {
		putc(c, F);
	}
	fclose(F);


	// opening the file in read mode
	F = fopen("abc.txt","r");
	while(1) {
		c = fgetc(F);
		if( feof(F) ) {
			break ;
		}
		// printing on console
		printf("%c", c);
	}
	
	fclose(F);

	return(0);
}

Output

stdio.h - putc() in c language

File's content

stdio.h - putc() in c language






Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





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 (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.