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()



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What's an object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (-c is a common "just compile; don't link" option.)

Ref : https://bit.ly/3CbzF8M