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

Passing arrays to functions in C programming

Like any other variables, we can also pass entire array to a function. An array name can be named as an argument for the prototype declaration and in function header. When we call the function no need to subscript or square brackets. When we pass array that pass as a call by reference because the array name is address for that array. /* Program to illustrate passing array to function */ #include<stdio.h> void display(int) ; /* function prototype */ main( ) { int num[5] = {100, 20, 40, 15, 33, i ; clrscr( ) ; printf (“\n The content of array is \n”) ; for (i=0; i<5; i++) display (num[i]) ; /*Pass array element fo fun */ getch{ } ; } void display(int n) { printf (“\t%d”, n ) ; } Output:     The content of array is 100      20       40       15 3 /* Program to read 10 numbers from keyboard to store these num into array and then c...

CPU (Central Processing Unit)

PGA reffered as pin grid array in which pins of CPU are lined up in a straight format. SPGA referred as staggered pin grid array. in which pins of CPU are arranged staggered format. LGA reffered to as Land Grid Array in which pins are available within inside the socket but not in the CPU in other words in line grid array, in CPU ther are no pinsbut insteadpins areattached with in a socket which contact with with the CPU. for e.g. LGA775 socket(no pins on cpu) has better cooling system. better contact and better locking(climbing). LGA1155: Letest generation I socket, also reffered as sandy bridge, Turbo boost overclocking. More resent than LGA 1156 socket. LGA1156: ability to north bridge Doul channel DDR3 optional integreted graphics PCI express LGA1366: hi end core i series socket integreted tiple channal memmory controller external control bridge(HUB) Upgrading CPU: need to check its core suppert check multiprocessor supports or not check on the ...

Recursive Function in C programming

             If a statement within the body of a function calls the same function, the function is called recursive function. Actually, recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. This process is used for repetitive computations in which each action is stated in term of previous result. Many iterative or repetitive problems can be written in this form.                To solve a problem using recursive method, two conditions must be satisfied. They are: 1)       Problem could be written or defined in term of its previous result. 2)       Problem statement must include a stopping condition. /*   An example of recursive function to calculate factorial of a number.*/    #include<stdio.h>    #include<conio.h...