w3resource

C putchar() function

C library function - putchar()

The putchar() function is used to write a single character to the standard output stream, writes a single character to the standard output stream, stdout.

Syntax:

int putchar(int char)

Parameters:

Name Description Required /Optional
char A single character write to o the standard output. Required

Return value

  • This function returns the character written as an unsigned char cast to an int or EOF on error.

Example 1: putchar() function

#include <stdio.h>

int main () {
   char C;

   for(C = 'A' ; C <= 'z' ; C++) {
      putchar(C);
   }
   
   return 0;
}

Output:

ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz

Example 2: putchar() function

Using the getchar() function, the following program reads characters into an array and prints them out using the putchar function once an end-of-file character is encountered.

#include <stdio.h>

 int main(void)
 {
   char text[500];
   int C, i, n = 0;
   printf("Input some characters:");
   printf("\nTo terminate press Ctrl+D on Unix/Linux terminals and Ctrl+Z in Windows console windows:\n");
   while ((C = getchar()) != EOF && n < 1000)
     text[n++] = C;
   printf("Write the said characters to the standard output:\n");	   
   for (i = 0; i < n; ++i)
     putchar(text[i]);
   putchar('\n');  
	
   return 0;
 }

Output:

Input some characters:
To terminate press Ctrl+D on Unix/Linux terminals and Ctrl+Z in Windows console windows:
C programming
C Exercises
C tutorial
^Z
Write the said characters to the standard output:
C programming
C Exercises
C tutorial

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous C Programming: C putc()
Next C Programming: C puts()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/stdio/c_library_method_putchar.php