C - Output character with putchar() function

Introduction

Here's the format:

#include <stdio.h> 

int putchar(int c); 

To make putchar() work, placing a literal character in the parentheses, as in

putchar('v'); 

Or you can place the ASCII code value (an integer) for the character in the parentheses.

The function returns the value of the character that's written.

Demo

#include <stdio.h> 

int main() /*from   w w  w.ja  v a  2  s  .  c  om*/
{ 
   int ch; 

   printf("Press Enter: "); 
   getchar(); 
   ch = 'H'; 
   putchar(ch); 
   ch = 'i'; 
   putchar(ch); 
   putchar('!'); 
   return(0); 
}

Result

This code above uses the getchar() function to pause the program.

The statement getchar(); waits for input.

The input that's received isn't stored; it doesn't need to be.

Then the putchar() function displays the value of variable ch one character at a time.

Single-character values are assigned to the ch variable.

Related Topic