Unformatted I/O in C

This type of input and output function works only with the character data types. Unformatted I/O does not require any format specifiers while reading from standard input devices and writing to standard output devices. The followings are the functions available for console I/O in C.

  • getchar()

This is the most basic function defined in ‘stdio.h’. This function reads and returns a single character from the standard input device (keyboard). In fact, it returns the ASCII value of the entered character. This function requires pressing ENTER to feed the character. It has the following form:

// prototype
int getchar(void);

// use case
char variable_name;
variable_name = getchar();
  • getch()

This is a non-standard function defined in ‘conio.h’. It is similar to getchar() except that it does not display the character entered and does not require ENTER to press. It has the following form:

// prototype requires conio.h to include
int getch(void);

// use case
char variable_name;
variable_name = getch();
  • getche()

This is a non-standard function defined in ‘conio.h’. It is similar to getchar() except it does not require pressing ENTER. It has the following form:

// prototype
int getche(void);

// use case
char variable_name;
variable_name = getche();
  • putchar()

This is a standard library function to display a single character in the console. It receives the character to display as an argument and returns the same character if the writing to the console is successful. It has the following form:

// prototype
int getchar(int character);

// use case
char variable_name = 'a';
putchar(variable_name);
// we can even check if the writing is successfull
  • gets()

This function reads the full string from the standard input device. It even accepts white spaces(single space, tab) in between the characters. It stops when the newline character is read or the end of the file is reached whichever comes first. This function returns NULL on error. It has the following form:

// prototype
char *gets(char *str);

// use case
char str[];
gets(str);
// str is pointer to an array of characters
  • puts()

This function prints the string to the standard output device(console). A newline character is appended to the output. On successful writing to the console, it returns the non-negative value. If an error occurs, it returns EOF. It has the following form:

// prototype
int puts(const char *str);

// use case
char str[] = "hello bitslord";
puts(str);

Example:

/*
    Program test for Unformatted I/O in C
*/

#include <stdio.h>
#include <conio.h>

int main(){
    char var_char, var_str[20];

    // testing getchar()
    printf("Testing getchar():\n");
    printf("Enter any character but it only accepts when you Enter ENTER:");
    var_char = getchar(); // this leaves \n in input buffer
    printf("Character you entered for getchar() is:");
    putchar(var_char);
    printf("\n\n");


    // testing getch()
    printf("Testing getch():\n");
    printf("Enter any character but it accepts when you press any character and does not appear:\n");
    var_char = getch();
    printf("Character you entered for getch() is:");
    putchar(var_char);
    printf("\n\n");

    // testing getche()
    printf("Testing getche():\n");
    printf("Enter any character but it accepts when you press any character and does appear on console:\n");
    var_char = getche();
    printf("\nCharacter you entered for getche() is:");
    putchar(var_char);
    printf("\n\n");

    // we already used putchar for example

    // testing gets() and puts
    // getchar() leaves ENTER character in input buffer so flush it
    fflush(stdin);
    printf("Testing gets() and puts():\n");
    printf("Enter any string containing white spaces, stops accept when new line/ENTER appears:\n");
    gets(var_str);
    printf("Character you entered for gets() is:");
    puts(var_str);
    printf("\n\n");

    return 0;
}

Sample Output:

Testing getchar():
Enter any character but it only accepts when you Enter ENTER:a
Character you entered for getchar() is:a

Testing getch():
Enter any character but it accepts when you press any character and does not appear:
Character you entered for getch() is:s

Testing getche():
Enter any character but it accepts when you press any character and does appear on console:
g
Character you entered for getche() is:g

Testing gets() and puts():
Enter any string containing white spaces, stops accept when new line/ENTER appears:
hello bitslord
Character you entered for gets() is:hello bitslord



Process returned 0 (0x0)   execution time : 17.919 s
Press any key to continue.