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++)
  {
10      putchar('-');
11    }
12    putchar('\n');
13    do {
14      puts ("Enter a string : ");
15      fflush(stdin);
16      gets (log_buffer);
17      puts ("Log text : ");
18      puts (log_buffer);
19      puts ("Want to repeat (Y/N): ");
20      fflush(stdin);
21      key = getchar();
22    } while(key == 'Y' || key == 'y');
23    for(= 0; i < 20; i++)
24    {
25      putchar('-');
26    }
27  }
--------------------
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
--------------------

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

 Vote 0

Similar topics related to this section

fseek, ftell, rewind, fprintf, fscanf, fflush, poll, select, fcntl, fputs,fgets,fgetc,fputc, printf, scanf, ioctl, mmap,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan