Input/Output Management in C Programming

Any data we feed into a program is called input. An input can be a file or data entered from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program.

Any data displayed by the program after execution of some commands is called output. Output can be displayed on the computer screen or printed on a paper or stored in a file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files.

Input and output function in C is divided into two categories namely formatted and unformatted. Functions like printf() and scanf() are formatted input and output functions while getch(), getche(), getchar(), gets(), puts(), putchar() are unformatted input output functions.

Formatted Input/Output Function

The input/output funtion which read and write all types of data values such as integer, float or character is called formatted input/output function. 

Example : printf(), scanf()

Unformatted Input/Output Function

The input/output function which read and write only character type data is called unformatted input/output function.

Example : getc(), getch(), getche(), getchar(), gets(), puts(), putchar()

scanf() and printf()

The standard input/output header file named stdio.h contains the definition of the functions scanf() and printf() which are used to take input from user and display output on the screen respectively. Format can be a simple constant string, but you can specify %s, %d, %c, %f, etc. to print or read strings, integer, character or float respectively. There are many other formatting options which can be used based on requirements.

Format Meaning
%d Scan or print an integer as signed decimal number
%f Scan or print a floating point number
%c Scan or print a character
%s Scan or print a string
#include <stdio.h>

int main(){
  int a;
  float b;

  printf("Enter an integer \n");
  scanf ("%d",&a);
  printf("Enter a float \n");
  scanf ("%f",&b);
  printf("control string \n");
  printf("%d%s%f",a,",",b);
}

getchar() and putchar()

The getchar() function reads a character from the terminal and returns it as an integer. This function reads only single character at a time. You must use this method in a loop to make it read more than one character. The putchar() function displays the character passed into it on the screen and returns the same character. This function also displays only a single character at a time. In order to display more than one characters, you must use putchar() method in a loop.

#include <stdio.h>

int main (  )
{
  char ch;
  printf("enter a character");
  ch=getchar( );
  printf("the input character is \n");
  putchar(ch);
  return 0;
}

gets() and puts()

The function gets() is used to read a string or a line of text from the input device and store them in a string variable. The function puts() is used to display the same text or string in the output device. 

#include<stdio.h>

void main()
{
  char name[30];
  puts("Enter your name");//printf("Enter your name ");
  gets(name);
  printf("My name is ");//piys("My name is");
  puts(name);
  return 0;
}

getch() and putch() 

The getch() and putch() functions are nonstandard function and are present in conio.h header file which is mostly used by MS-DOS compilers. They are not part of the C standard library. The getch() function is used to read a single character while the putch() function is used to display a character on the output device.

#include<stdio.h>

int main()
{
  char ch;
  printf("enter a character \n");
  ch=getch();
  printf("\n the input character is ");
  putch(ch);
}

Take password of 8 characters and display * for each character at the beginning. Show the password after execution.

#include<stdio.h> 

int main()
{
  // Taking the password of 8 characters
  char pwd[9];
  int i;

  printf("Enter Password: ");
  for (i = 0; i < 8; i++) {

  // Get the hidden input using getch() method
  pwd[i] = getch();

  // Print * for each character entered
    printf("*");
  }
  pwd[i] = 0;
  printf("\n");

  printf("Entered password: ");
  for (i = 0; pwd[i] != 0; i++){
    printf("%c", pwd[i]);
  }  
  // Now the console will wait for a key to be pressed
  putch(pwd);
}

getc() and getche()

The function getc() reads a single character from a given input stream and returns the corresponding integer value on success.

#include <stdio.h>

int main()
{
   printf("Enter a character \n");
   printf("%c", getc(stdin));
   return(0);
}

The function getche() reads a single character from the keyboard and displays immediately on output screen without waiting for enter key.

#include <stdio.h>

int main()
{
  printf("Enter a character \n");
  printf("\n%c", getche());
  return 0;
}


0 Like 0 Dislike 0 Comment Share

Leave a comment