Getchar and Putchar Function in C with Example

Introduction

In this post, I am going to explain about getchar and putchar function in c programming with examples. These are are the unformatted input-output functions in c programming. There are two types of input-output functions in the c programming. One is formatted and another is unformatted functions.

Also Read: C Language Program to Count the Number of Lowercase Letters in a Text File

Formatted input-output functions can read or write any format like integer, float or character and Unformatted means only one format i.e. characters only.

Formatted input-output functions are scanf() and printf() and unformatted functions are getchar(), putchar(), gets(), puts(), etc.

In this post, we are going to learn only about getchar and putchar functions.

What is the getchar in c?

This is the input function in c programming. We can read only a single character using this getchar function from the console. See the following syntax.

Also Read: Reverse a Number in C

char getchar();

From the above syntax, the return type of getchar function is char. That means, it can read only character value. For example, ch=getchar(); will read a single character from a console and store that character in the character variable ch.

What is putchar in c language?

This is output function in c programming which display a single character to the console. It has the following syntax.

int putchar(char);

from the above syntax, you can see that return type of putchar function is int. That means it returns the ASCII value of the variable char that we are displaying on the console. For example, suppose, we have a integer variable i and the statement i=putchar(‘a’); will display the character ‘a’ to the console and the value of i will be 97. The ASCII value of ‘a’ is 97.

Also Read: C Program to Find the Sum of Cubes of Elements in an Array

Before writing a c program to reverse a number, let us see the simple program to read and write a single character using getchar and putchar in c.

C Program to Read and Write a Single Character

In this program, we are just reading a single character and displaying the same character on the console.

#include <stdio.h>
#include <stdlib.h>
main( )
{
 char ch;
 printf("Enter a character: ");
 ch=getchar();
 printf("You have entered a character: ");
 putchar(ch);
 return 0;
}

Output

Enter a character: C
You have entered a character: C

Program to print reverse number/ string using getchar and putchar in c

In this program, we are reading a number from the user and then we will reverse that number. But, here we are using getchar and putchar functions in c. Now, see the following program.

Also Read: Operators in C with Detailed Explanation

#include <stdio.h>
#include <stdlib.h>
main( )
{
 char ch;
 char s[10];
 int i=0;
 printf("Enter a number or string\n");
 while((ch=getchar())!='\n')
 {
    s[i]=ch;
    i++;
 }
 printf("Original Number/String: %s\n",s);
 printf("Reverse Number/String: ");
 while(--i>=0)
 {
     putchar(s[i]);
 }
 return 0;
}

The above program is reversing a number. Here, we have used two while loops. In the first while loop, we are reading a number using the getchar() function and in the second while loop, we are displaying a reversed number using the putchar() function.

See the following output.

Enter a number or string
12345
Original Number/String: 12345
Reverse Number/String: 54321

Also Read: C Program to Display Middle Row and Column of Matrix

I hope you have understood the concept of getchar() and putchar() functions. If you have any difficulties, then please feel free to contact me.

Thank you.

FAQ

What is the difference between getchar and putchar?

The getchar() reads the single character from the console and putchar() prints or writes the single character to the console.

Write a c program accept a character and check it is character G or not,

int main()
{
char ch;
printf(“Enter a character: “);
ch = getchar();
if(ch == ‘G’)
{
printf(“You have entered character G”);
}
else
{
printf(“Please enter character G”);
}
return 0;
}

1 thought on “Getchar and Putchar Function in C with Example”

Leave a Comment