i/o and o/p functions in C

The input/output functions in C allows to transfer information between computer and standard i/p, o/p devices. There are many built-in functions in C language to read given input and write data on output devices. Most of data i/p and o/p functions are defined in the header file <stdio.h>. In this chapter we will study to use about some of these inut/output functions.

  • i/p functions –    getchar(), scanf(), gets() Used to take information’s from standard device like keyboard.
  • o/p functions –    putchar(), printf(), puts() Used to write information’s to the standard device like system monitor.

The getchar() and putchar() functions :-

getchar() is use to read only single character at a time and putchar() is use to write only single character at a time, see an example below. we can use loop, if we want to read/write more than one character. We wiil see about loops in chapter (2. C Flow Control – decisions and loops).

#include <stdio.h>
int main()
{

char  alphabet;printf(“Enter a value:\n”);

/* Here reading first single character entered  by you */
alphabet  =  getchar();

printf(“You have entered :\n”);

/* Here writing on screen first single character entered  by you */
putchar( alphabet );

return 0;

}

When we compiled and executed  above C program, After displaying “Enter a value” program wait for you to enter any value, suppose you entered ‘A’ the output will be:-

$  ./a.out
Enter a value
A
You have entered :
A

Suppose you have entered a sentence like “Welcome to gyantoday.com” then getchar() will take first character of the sentence, the output will be:-

$  ./a.out
Enter a value
Welcome to gyantoday.com
You have entered :
W

The scanf() and printf() functions :-

scanf() is used to read any combination of numerical values, single character and strings. It returns the number of data items that have been entered successfully and printf() is used  to write any combination of numerical values, single character and strings. It returns the number of data items that have been write successfully.

syntax of scanf() function :-

scanf(control string, arg1, arg2, arg3, ……., argn);

Here control strings required formality information. It is individual group of characters where each character group must begin with %(ampersand) followed by conversion character.
and arg1, arg2 .., aarg3 is argument that represent the individual i/p data items.

Given below conversion characters and their purpose of use

Conversion Character Meaning
%c single character
%d Decimal number
%f Floating point number
%e Floating point value in scientific notification
%g Floating point value either f-type or e-type
%h Short integer
%i Decimal, Octal or Hexa decimal
%o Octal number
%s String
%u Unsigned integer
%x Hexadecimal

Lets see an example C program for scanf() and printf()

#include <stdio.h>
int main()
{

char  alphabet;

printf(“Enter a value:\n”);

/* Here reading first single character entered  by you */
scanf(“%c”,&alphabet);

/* Here writing on screen first single character entered  by you */
printf(“You have entered : %c\n”, alphabet);

return 0;

}

When we execute above C program, After displaying “Enter a value” program wait for you to enter any value, suppose you entered ‘A’ the output will be:-

$  ./a.out
Enter a value
A
You have entered : A

 

#include <stdio.h>
int main()
{

int  number;printf(“Enter a value:\n”);

/* Here reading an integer value entered  by you */
scanf(“%d”,&number);

/* Here writing on screen an integer value entered  by you */
printf(“You have entered : %d\n”,number);

return 0;

}

When we execute above C program, After displaying “Enter a value” program wait for you to enter any value, suppose you entered ‘100’ the output will be:-

$  ./a.out
Enter a value
100
You have entered : 100

The gets() and puts() functions :-

gets() is used to read a line of chracters from standard input devices until either a terminating newline or EOF encounter.  puts() is used to write a line of characters  and a tailing a newline to standard  output devices. The library functions gets and puts operate on stdin and stdou, gets deletes the terminating ‘\n’, and puts adds it.

Let see an example C program

#include <stdio.h>
int main()
{

/* Declaring one character array of size 100,
We will study about array in detail in chapter 4 */
char  alphabets[100];printf(“Enter a value:\n”);

/* Here reading a line of characters entered  by you */
gets( alphabets );

/* Here writing on screen, a line of characters entered  by you */
puts( alphabets );

return 0;

}

When we compiled and executed  above C program, After displaying “Enter a value” program wait for you to enter any value, suppose you have entered a sentence like “Welcome to gyantoday.com” the output will be:-

$  ./a.out
Enter a value
Welcome to gyantoday.com
Welcome to gyantoday.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.