Thursday, 15 February 2018

Input and Output functions in C++

The C language did not build the input/output facilities into the language. In other words, there is no keyword like read or write. Instead, it left the IO to the compiler as external library functions (such as printf and scanf in stdio library). The ANSI C standard formalized these IO functions into Standard IO package (stdio.h). C++ continues this approach and formalizes IO in libraries such as iostream and fstream.

  • C++ IO is type safe. IO operations are defined for each of the type. If IO operations are not defined for a particular type, compiler will generate an error.
  • C++ IO operations are based on streams of bytes and are device independent. The same set of operations can be applied to different types of IO devices

C++ uses the concept of stream and stream classes to manipulate its I/O operations with the console and disk files. There are three types of I/O functions in C++
  • Unformatted I/O functions
  • Formatted Console I/O functions
  • File I/O Functions.
The two major I/O stream functions used with any C++ program are cin and cout. These functions are defined under iostream header file.

The following is the general format for reading data from the keyboard.
cin>> variable1 >> variable2>>.........>>variable N
variable1, variable2 .... are valid C++ variable names that have been declared already. This statement will cause the computer to stop the execution and look for input data from the keyboard. The operator >> reads the data character by character and assigns it to the indicated location. The reading of variable will be terminated at the encounter of a white space or a character that does not match the destination types. For example, consider the following code.

int a;

Suppose "10" is given as input, the operator will read the value and assign to a.

The general form for displaying data on the screen is:
cout<< item1 << item2 << ..........<< item N
The items may be variables or constants of nay basic type.


Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial


C Programming


Java Tutorial


Data Structures


MS Office


Database Management