Unformatted text input and output means either single character input or output or input output of null terminated strings. There are four library function available and they are getchar, putchar, gets and puts.

getchar

getchar() - read one single character from console. This function is blocking and waits for return key press. It returns immediately after enter key is pressed. Return value is the ASCII key which is equivalient to the key press.

putchar

putchar() - writes a single character to the console. It is a blocking function and waits until character is written to the console file and returns the same value.

gets

gets() - reads characters from stdin and returns when new line character is reached. This is a blocking function. It takes a character buffer and populates characters as it reads from stdin. It puts a null character instead of new line when it receives enter key press.

puts

puts() - It takes a constant null terminated string and copies the buffer to stdout. It also puts an extra newline character when it reaches end of input string. Output is visible in the console. This function is blocking and returns when all buffer characters are copied to stdout.

Source Code

#include <stdio.h>

int main(int argc, char *argv[])
{
  int i;
  char log_buffer[100];
  char key;
  for(= 0; i < 20; i++)
  {
    putchar('-');
  }
  putchar('\n');
  do {
    puts ("Enter a string : ");
    fflush(stdin);
    gets (log_buffer);
    puts ("Log text : ");
    puts (log_buffer);
    puts ("Want to repeat (Y/N): ");
    fflush(stdin);
    key = getchar();
  } while(key == 'Y' || key == 'y');
  for(= 0; i < 20; i++)
  {
    putchar('-');
  }
}
--------------------
Enter a string :
this is a text1
Log text :
this is a text1
Want to repeat (Y/N):
Y
Enter a string :
this is a text2
Log text :
this is a text2
Want to repeat (Y/N):
N
--------------------

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#