Home » C programs » stdio.h header file functions

putchar() function in C language with Example

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

putchar() function in C

The putchar() function is defined in the <stdio.h> header file.

Prototype:

    int putchar(const char *string);

Parameters: const char *string

Return type: int

Use of function:

In the file handling, through the putchar() function, we take the character to the stream stdout and store them into the specified string array. The prototype of the function putchar() is int putchar(const char *string);

The character which is read is an unsigned char which is converted to an integer value. In the case of file handling, it returns EOF when end-of-file is encountered. If there is an error then it also returns EOF.

putchar() example in C

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

int main(){
	//Initialize the character array
	char str[100];
	int i=0,j=0;
	
	printf("Enter the string into the file\n");
	//takes all the characters until enter is pressed
	while((str[i]=getchar())!='\n'){
		//increment the index of the character array
		i++;
	}
	//after taking all the character add 
	//null pointer at the end of the string
	str[i]='\0';
	printf("\nThe file content is - ");

	//loop is break when null pointer is encountered
	while(str[j]!='\0'){
		//print the characters
		putchar(str[j]);
		j++;
	}
	
	return 0;
}

Output

putchar() example in C language






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.