C Programming

Unformatted I/O

Posted on 16th October 2014

C provides various function to perform formatted and unformatted i/o operations. The difference between formatted and unformatted input and output operations is that in case of formatted I/O the data is formatted or transformed. Unformatted I/O transfers data in its raw form or binary representation without any conversions. Unformatted I/O is the most basic form of I/O and it is simple, efficient and compact. It is portable only between machines that use the ASCII character set, which is what most computing devices use these days.

In formatted I/O binary data is converted to ASCII before being stored, manipulated or output. Formatted I/O is always portable and human readable, but the downside is it uses more space and it is computationally expensive because of the conversions to be done in between input and output. The scanf() and printf() functions are examples of formatted data input and output respectively. Similarly you can use the functions fscanf()and fprintf() for formatted input and output to files

C provides many low level functions to read and write unformatted data. These functions are explained next.

getchar() and putchar()

getchar() function will read a single character from the standard input. The return value of getchar() is the first character in the standard input.The input is read until the Enter key is pressed, but only the first character in the input will be returned.

putchar() function will print a single character on standard output. The character to be printed is passed to putchar() function as an argument. The return value of putchar() is the character which was written to the output.

getchar() and putchar() functions are part of the standard C library header stdio.h

Example

#include <stdio.h>
int main()
{ 
 char ch;
 printf("Input some character and finish by pressing the Enter key.\n");
 ch = getchar();
 printf("The input character is ");
 putchar(ch);
 return 0;
}

If we run this program and enter the characters "apple" at the prompt then the output will be as shown below


Input some character and finish by pressing the Enter key
apple
The input character is a

Please note that when you entered the word apple those characters were echoed on the screen. You also terminated the input by pressing the Enter key. However getchar() function took only the first character, which is "a" from the input and putchar() function printed it out.

getch(), getche() and putch()

These functions are similar to getchar() and putchar(), but they come from the library header conio.h. The header file conio.h is not a standard C library header and it is not supported by compilers that target Unix. However it is supported in DOS like environments.

getch() reads a single character from the standard input. The input character is not displayed (echoed) on the screen. Unlike the getchar() function, getch() returns when the first character is entered and does not wait for the Enter key to be pressed.

getche() is same as getch() except that it echoes the input character.

putch() writes a character that is passed as an argument to the console.

Example

#include <conio.h>
#include <stdio.h>
int main()
{
 char ch;
 printf("Press any key\n");
 ch = getch();
 printf("The key pressed is: ");
 putch(ch);
return 0;
}

If we run this program and press any key, let's say "x", then the output will be as shown below:


Press any key
The key pressed is x

Please note here that the character "x" was not echoed when you entered it.

Post a comment

Comments

ahia | January 3, 2018 9:11 AM |

so whats the diffrence between putch and putchar?

Sreedhar | January 3, 2018 12:15 PM |

There isnt any difference in functionality between putchar and putch. But putch is not supported by all C compilers and it is part of conio.h header file