Basic Input Output




















Basic input/output:- Input refers to accepting data from outside the program to the program while output refers to presenting the programmed data as a result of programming. Every program performs three main functions accepting data from user, processing it and producing the output.

In C language there are several input and output functions.These functions are collectively found in  "IO.h" and such IO function together form a library name "stdio.h" .User will require all such library function.
These library functions are classified into three broad categories:


1. Console I/O functions:- Console I/O refers to the operation that occur at the keyboard and the screen of your computer . Different streams are used to represent different kinds of data flow. In C there are 3 streams associated with console I/O operations. These are:-

Stdin:-   It is the stream which supplies the data from input sources(i.e keyboard) to program.
Stdout:- It is the stream which is used to receives output of a program & supply it to output device.
Stderr:- It is the stream used to keep error messages separate from the output of a program.

Console input output can be classified in two parts:

1.Formatted console input output.
2.Unformatted console input output.

1. Formatted console I/O:- Formatted I/O function accepts or present in a particular format. The example of formatted console I/O function is printf( ) and scanf( ).

printf( ):- It is highly desirable that the output are presented in such a way that they are understandable and are in a form easy to use.
The printf( ) statement provides certain features through which the screen output is effectively controlled.
The general form of printf() function is:- printf("Control String ",arg1,arg2....);

Ex:- #include<stdio.h>
       void main()
       int a=10;
       printf("My roll number is %d", a);

 =>>It will produce the output :- "My roll number is 10".

 Control string may contain:
i. character that are simply printed as they are.
ii. Conversion specification that begin with a sign.
iii. Escape sequence that begin with \ sign.

scanf:- scanf allows formatted reading of data from the keyboard. Like printf it has a control string, followed by the list of items to be read.Scanf is a conversion charcter that can be used with printf() function.
However scanf wants to know the address of the items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by "&" sign.Character strings are an exception to this. Since a string is already a character pointer, we give the names of string variables unmodified by a leading &.Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalents.
It Accepts the following format:=   "   scanf("format string", arg1,arg2......argn);  ".

Ex:- #include<stdio.h>
       void main()
       int a;
      printf("Enter any number:-  ");
      scanf("%d", &a);                     /*Here %d is conversion specifier which specify the data type of value to                                                                                        
      printf("%d", a);                           receive from the keyboard. And "a" name of memory location  which
                                                       stores the input from user. & keeps the data to the address of a. */

2. Unformatted console I/O:- This function cannot control the format of reading and writing the data. There are two types of unformatted console Input/Output functions:-

  • Character I/O functions.:- These are the functions that can input/output  of one character at a time. These functions deal with the individual character value . For inputting , following functions are used:-  i)   getchar( ) :- This is used for reading a character from the keyboard. Its syntax is:-                                                 "Character_variable  = getchar()"  here character_variable is any valid C variable name. Ex.:- ch=getchar( ), This statement accept a character and stores it to "ch". In this function the typed character  is echoed to the monitor and waits for carriage return(Enter) to assign it to variable.                                                                                                                                      ii)  getch( )  :- It also accepts a single character. And its syntax is also "Character_variable  = getch()".  It doesn't display the character at the time of typing. And also it doesn't require enter to assign value to variable.                                                                                                                                          iii) getche( ) :- It is same as getch( ) but only difference is it it echoes the character at the time of typing on the monitor. its syntax is :- "Character_variable  = getche( )".                                               
      For Output of single character there are two functions:- 
         i) putchar( ) :- It is used to display a character.. Its syntax is                                            "putchar(character_variable/'character')"
         ii) putch( ) :- It is also used to display a single character on the screen. Its syntax is "putch(variable name);" 
  • String I/O functions.:- These are the functions that  is used to input/output of whole string. Following functions are used to input and output in program:-   gets( ) and puts( ).

i) gets:- gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines. When all input is finished, NULL as defined in stdio.h is returned.
 Its syntax is "gets(character Array)".
ii) puts:- puts writes a string to the output means it displays the character on the screen, and ends it with a newline character.Its syntax is :- "puts(character array)"

2. Disc I/O functions:- Function which performs secondary storage devices like floppy disk or hard disk is called disk I/O functions.

3. Port I/O functions:- This type of functions performs I/O operation among various codes like printer port, mouse port.

Comments

Popular posts from this blog

Components of C language

Decision making using if statement

How to make payment for final certificate, migration, provisional for the students of last semester of ptu?