17.3 – Unformatted Console I/O

by subbu on October 8, 2014

Reading and writing characters

C language provides number of unformatted functions like getchar(), getche(), getch(), putchar() and putch() to read and write characters on to the Console IO. Though, theses sound similar, differs in functionality

getchar() function

It is a line-buffered input function. Here the meaning of buffer is temporary storage. When we use getchar(), all the characters we enter will be stored in the buffer until user shows confirmation by pressing enter. The characters stored in the buffer would be accessed by the getchar() one by one. It is generally used where a line of text need to be accessed character by character

getchar() function

Though getchar() returns ASCII value of accepted character, it can be directly assigned to a char type variable.

buffered input function

According to the above picture, on pressing enter button the word “codingfox” would go and store in the buffer. Now the getchar() reads character by character as loop progresses

#include<stdio.h>
int main()
{
 char ch;
 while(1) /* Infinite Loop */
 {
  ch=getchar();   /* reads from buffer */
  if(ch=='.')     /* terminating character */
    break;
  printf("%c",ch); /* writes on to monitor */
 }
 return 0;
}

Execution
codingfox.
codingfox

getch() function

Though it is also used to accept a character from the keyboard, functionally it is different from getchar().
getch() doesn’t echo (show) the typed character on the monitor and straightaway reads the character rather buffering

Note: getch() is not supported by gcc

#include<stdio.h>
int main()
{
 char ch;
 while(1) /* Infinite Loop */
 {
  ch=getch();
  if(ch=='.')     /* terminating character */
    break;
  ch=ch-32;       /* converting to upper case */
  printf("%c",ch); /* writes on to monitor */
 }
 return 0;
}

Execution
CODINGFOX

Input to the above program is “codingfox” (small case) but could not see the input because getch() doesn’t echo the typed characters but printing “CODINGFOX” (upper case) which is the output printed by the printf() after converting to upper case.

getche() function

It is a similar function to getch(), used to accept a character from the console input but slightly differs in its functionality. It reads the character directly without buffering but echoes (shows) the given character.

Note: It is not supported by gcc

#include<stdio.h>
int main()
{
 char ch;
 while(1) /* Infinite Loop */
 {
  ch=getche();
  if(ch=='.')     /* terminating character */
    break;
  ch=ch-32;       /* converting to upper case */
  printf("%c",ch); /* writes on to monitor */
 }
 return 0;
}

Execution
cCoOdDiInNgGfFoOxX.

In the above execution all the small case characters are input characters and all the capitals are printed characters by printf()

putchar(), puch() function

Both these functions are used to write a character onto console output. Though these accept integer as argument, we can also send a character as an argument. It returns the ASCII value of a printed character in return. It returns -1 if any thing goes wrong with the writing.

Note: putch() is not supported by gcc

putchar() function

#include<stdio.h>
int main()
{
 char ch;
 while(1) /* Infinite Loop */
 {
  ch=getchar();
  if(ch=='.') /* terminating character */
    break;
  ch=ch-32;   /* converting to upper case */
  putchar(ch); /* writes on to monitor */
 }
 return 0;
}

Execution
codingfox.
CODINGFOX

In the abobe example gatchar() is used to read buffered input characters from the keyboard and putchar() is used to print the characters on to the monitor

#include<stdio.h>
int main()
{
 char ch,dh;
 ch='a';
 dh=putchar(ch);
 putchar('\n'); /* next line */
 putchar(dh);   /* printing returned value */
 return 0;
}

Output
a
a

putchar() is retuning the printed character ‘a’, which is also being printed separately

gets() function

As we have learned earlier under the session 15.3 More about strings scanf() could not access a line of text with spaces from the keyboard and store into the array, only the first word can be accepted and stored
The only way to accept a line of text from the keyboard including with spaces is gets().

gets() function

The gets() function accepts the address of any character array, stores the line of text accepted from the keyboard character by character and a terminating character ‘\0’ is automatically added.
It also returns the address of string accepted from the keyboard

#include<stdio.h>
int main()
{
 char x[20],*p;
 printf("Give a line of text:");
 p=gets(x);
 printf("The given text: %s\n",x);
 printf("String through returned pointer: %s\n",p);
 return 0;
}

Execution
Give a line of text: igate solutions
The given text: igate solutions
String through returned pointer: igate solutions

Take care while using gets()
Care must be taken while using gets() because it would not perform bound checking (checking the size) before storing the string into the character array. If user gives a string whose length is more than the size of array then it may overwrite other data rather giving error
Hence gcc gives warning while compiling a C program with gets() like

Warning: The ‘gets’ function is dangerous and should not be used”

puts() function

It is used to print a string onto the console output; it accepts the address of character array or string as argument and prints character by character on the monitor until the end of character ‘\0’.
It returns -1 if any thing goes wrong while writing, returns a non-negative on successfully writing the data on to the monitor
One more good thing with the puts() is that, it automatically introduces a new line character

puts() function

#include<stdio.h>
int main()
{
 char p[]="www.codingfox.com";
 int x;
 x=puts(p);
 if(x>=0)
  printf("Successfully printed");
 return 0;
}

Output
www.codingfox.com
Successfully printed

Previous post:

Next post: