Skip to main content

Input and Output(I/O) Functions in C

            Reading the data from the input devices and displaying the result on the screen, are the two main task of any program. To perform these tasks, C has a number of input and output functions. When a program needs data, it takes the data through the input functions and sends results obtained through the output functions. Thus the input/output functions are the link between the user and the terminal. There are number of input/output functions in c based on the data types. The input/output functions are classified into two types.
1) Formatted functions               Input →| Program |→Output
2) Unformatted functions.

1) Formatted Functions:
The input function scanf() and  output function printf() fall under this category. These formatted functions allow us to input or output in a fixed or required format. For example, while displaying value of certain variable, we can specify the number of decimal places after decimal points, number of spaces before the value, the position where the output is to be displayed etc.

printf() function:
Printf() is a built in function which is used to output data from the computer onto a standard output device i.e. monitor’s screen. This function can be used to output any combination of numerical values, single character and strings. Generally, the printf() function is written as
Printf(control string, arg1, arg2, arg3,…..,argn);
Where control string refers to a string that contains formatting information and arg1, arg2, arg3,……,argn are arguments that represent the individual data items. The arguments can be written as constants, single variable or array names or more complex expression. Here, control string consists of individual groups of character, with one group of character for each output data item. Each character group must begin with a percent sign (%). The control string can be written in following form.

Control string= %[flags][field width][.precision] conversion character
Where the items within [] are optional and % and conversion character are
compulsory.

Flags: The flags affect the appearance of the output. The flags must be placed
immediately after the percent sign. The flags may be -, +, 0
-          : - data item is left justified.
+    : - A sign will precede each signed numerical data item.
0        : - Leading 0s are appeared instead of leading blanks.
e.g. let  a=45;
printf(“%05d”);
Output: 00045 // leading 0s

Field width:
It sets the minimum field width. If the number of characters in the corresponding data item is less than the specified field width, then the data item will be preceded by enough leading blanks to fill the specified field. If the number of characters in the data item exceeds the specified field width, then additional space will be allocated to the data item so that  the entire data item will be displayed.
e.g.
a=45;
printf(“%5d”,a);
output: - - -45   i.e. minimum width is 5. (here – represents space)

precision:
It sets the number of digits after decimal point.
a=45.674;
printf(“%.2f”,a);
Output: 45.67

Conversion character:
d for integer data
f for float data
c for character
etc……

scanf() function:
This built-in function can be used to enter input data into the computer from a standard input device i.e. keyboard. The function can be used to enter any combination of numerical values, single characters and strings. In other word, it is used for runtime assignments of variables. In general term, the scanf() function is written as
scanf(control string, arg1, arg2, ….,argn);
Where control string refers to a string containing certain required formatting information and arg1, arg2, ….,argn are arguments that represent the individual input data items. The arguments represent pointer that indicate the address of the data items within computer memory. Thus, it is preceded by ampersand i.e. &.
The control string consists of individual groups of characters, with one group for each input data item. Each character group must begin with percent sign (%).

Control string= %[field width] conversion character.

Filed width:
It limits the number of input characters (i.e. it sets the maximum number of characters to be entered). This is an unsigned integer which is placed within the control string between the % and the conversion character. The data item may contain fewer characters than the specified field width. However, the number of characters in the actual data item can not exceed the specified field width. Any characters that extend beyond the specified field width will not be read. This is optional field in scanf() function.
e.g.        int a;
scanf(“%3d”,&a);
  printf(“\n%d”,a);
Output:
23456   // say input data by user
234     //output

Conversion character:
It is same as in the case of printf() function.

An example showing its syntax:
scanf (“%d %f %c”, &a, &b, &c);
Where’ &’ is the address operator, also called ampersand operator used to indicate the memory location of defined variables and a is integer, b is float and c is character type variable.
When user enters inputs as 34 89.9  r, then it assigns variables as
a=34, b=89.9 and c=’r’


2) Unformatted Functions:
There are several functions under this category which don’t allow us to enter or display the various data items in required format. The functions getchar(), putchar(), gets(), puts(), getch(), getche(), putch() are considered as unformatted functions.

getchar():
It returns a single character from a standard input device. In general term, it is written as
character variable = getchar();
Here, character variable refers to some previously declared character
variable.

putchar();
This functions prints one character on the screen at a time which is read by the standard input.In general term, it is written as
putchar(character variable);
Here, character variable refers to some previously declared character variable.
e.g.
char c;
c=getchar();
putchar(c);
Output:
   // input
r    // output

getch(), getche() and putch():
The functions getch() and getche() read single character the instant it is typed without waiting for the enter key to be hit. The difference between them is that getch() just reads the character that typed without echoing it on the screen and  getche() reads the character and it echoes (displays) the character that typed to the screen.
Again, the function putch() prints a character taken by the standard input device
e.g.
char c;
c=getch();
putch(c);
Output:
r     //output
char d;
d=getche();
putch(d);

Here getch() and getche() accepts entered key and putch()  displays the pressed value.

gets() and puts():
gets() offer alternative function of scanf() function for reading strings. Similarly, puts() offer alternative function of printf() function for displaying the string. In general term, they are written as:
gets(string variable);
and puts(string variable);

Here, each function accepts a single argument. The argument must be a data item that represents a string. In case of get(), the string will be entered from keyboard and will terminate with a new line character (the string will end when the user presses the enter key).
Example
      #include<stdio.h>
      void main()
    {
      char line[80];
      gets(line);
      puts(line);
       }
 


This program transfer the line of text into the computer using gets() and display the line using puts().

Comments

Popular posts from this blog

Database Management System(DBMS)

Data and Information  Data is an unprocessed raw fact which does  not give complete meaning about any object,person, entity etc. But when various inter-related data arranged in a row, it gives a complete meaning which is referred to as information. For Example: ID Name Address Program 1101 Ram Shrestha Biratnagar BScIT         is information of a specific person. Database Database is a repository(storage) for the organized collection of related information  of data items. It Consist of meaningful arrangement of data, relationship, constraints, schema etc. In modern days we define database as a computerized record-keeping system used to store information and allows users to retrieve and update these information on their request. Some example of databases are telephone directory, data of SLC result, student and employee records etc. Database Management System(DBMS) Database is the softw...

What is Java Applets?

An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. Furthermore, an applet is downloaded on demand, without further interaction with the user. If the user clicks a link that contains an applet, the applet will be automatically downloaded and run in the browser. Applets are intended to be small programs. They are typically used to display data provided by the server, handle user input, or provide simple functions, such as a loan calculator, that execute locally, rather than on the server. In essence, the applet allows some functionality to be moved from the server to the client.

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...