getchar(), putchar(), gets() and puts()

The Console IO functions are :

  • getchar()
  • putchar()
  • gets()
  • puts()

where getchar() and putchar() are single character functions and
gets() and puts() are string functions.

getchar putchar gets puts

Header files for these above functions : “stdio.h”

Single Character Functions : getchar() and putchar()

Single Character functions are used for reading and displaying a single character respectively. These functions are :

  • getchar() – reads a character from the keyboard.
  • putchar() – prints a character to the screen.
Example of getchar() and putchar() :
character = getchar();
  • The statement 1 reads a character and stores it in variable character.
  • So, it waits for the character input until a character is typed at the keyboard.
putchar(b);
  • The statement 2 prints the given character on the screen at the current cursor position.
  • So, it will display a character (‘b’) on the screen.
Another Example of getchar and putchar :
char ch;
ch = getchar();
putchar(ch);
  • The getchar() waits for the character input until a character is typed at the keyboard.
  • Whatever is stored inside ch, gets displayed on the screen.

String Functions : gets() and puts() :

The single character functions defined above can handle single character at a time and cannot handle multiple characters. To solve this problem, there are functions that can handle strings also. These functions are :

  • gets() – accepts a string of characters entered at the keyboard and places them in the string variable mentioned with it.
  • puts() – writes a string on the screen and advances the cursor to the newline. i.e. it ends with a new line character.
Example of getchar() and putchar() :
char name[16];
gets(name);
  • The statements 6 & 7 declares a string namely ‘name’ which can store 15 valid characters (16th – for storing null ‘\0’).
  • gets() read a string of maximum 15 characters and stores it in memory address pointed to by ‘name’.
  • null terminator ‘\0’ is automatically placed at the end of the string, if the enter or carriage return is pressed.
puts("one");
puts("two");
  • The above code will print “one” and “two” in separate lines because
    • the first puts() will print “one” and place the cursor at the new line.
    • the second puts() will print “two” appears on this line and again the cursor is advanced to newline.
    • Any subsequent output will appear on the next line.

Program to differentiate between single character functions and multi character functions/string functions :

#include<iostream.h>
#include<stdio.h>
int main()
   {
    int num, spaces = 0, others = 0, words = 1;
    cout<<"choose to display"<<endl
            <<"1. Number of spaces and other characters \n"
            <<"2. Number of Spaces and words\n";
    cin>>num;
    if(num == 1)
     {
      char ch;
      cout<<"Enter a string :";
      while (ch = getchar() != '\n')
         {
          if(ch == ' ')
             spaces++;
          else
             others++;
         }
      cout<<"spaces = "<<spaces<<endl;
      cout<<"other characters = "<<others<<endl;
     }
    else
      {
        char strng[81];
        puts("Enter a string (max of 80 characters)");
        gets(strng);
        for(int i=0; strng[i]!='\0'; i++)
        if(strng[i] == ' ')
           spaces++;
        else 
           words++;
       }
    return 0;
   }
     

Incoming search terms:

  • disadvantages of intrapreneurship
  • difference between gets and puts
  • difference between gets and puts in C language
  • Imperative programming the difference between gets and puts
  • difference between gets and puts in c
  • difference between getchar and pitchar
  • differentiate between gets and puts io functions
  • getchar putchar gets puts
  • getchar() putchar() gets() puts()
  • getchar n putchar difference in c

Leave a Reply