Tech Study

c++ Basic Input And Output With Examples

C++ Basic Input And Output

We will see that the C++ standard libraries provided us with an extensive set of input/output capabilities. Here we are going to discuss very basic and most common c++ basic input and output operations that are required for c++ basic input and output programming.

C++ basic input and output occur in streams, which generally are nothing but sequences of bytes. When there occurs a situation where bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, then that can be called input operation and a situation occurs where bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., then that can be called as output operation.

I/O Library Header Files:-

Below we are going to find header files that are important to C++ programs −

<iostream>

This file generally defines the cin, cout, cerr and clog objects, which are supposed to correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively.

<iomanip>

This file is provided to declare services that are useful for performing formatted I/O with so-called parameterized stream manipulators, such as setw and setprecision.

<fstream>

This file is provided to declare services for user-controlled file processing.

The Standard Output Stream (cout):-

Cout is a predefined object which is an instance of the ostream class. The cout object is generally supposed to be “connected to” the standard output device, which on a common practice is the display screen. The count is always used along with the stream insertion operator, which is written as << which are two less than signs which can be seen in the following example.

#include <iostream>

using namespace std;

 int main() {

   char ristr[] = "Hello! Rishu Here";

    cout << "Value of ristr is : " << ristr << endl;

}

When the above code is compiled and executed, we will find the following produced result  :-

Value of str is : Hello! Rishu Here

The data type of variable to be output and the appropriate stream insertion operator to display the value are also determined by C++ compiler. 

The Standard Input Stream (cin):

Cin is a predefined object which is an instance of stream class. The cin object is generally supposed to be attached to the standard input device, which on a common practice is the keyboard. The cin is always used along with the stream extraction operator, which is written as >> which are two greater than signs which can be seen in the following example.

#include <iostream>

 

using namespace std;

 

int main() {

   char riname[50];

   cout << "Tell me your name: ";

   cin >> name;

   cout << "Your good name is: " << riname << endl;

}

It will prompt you to enter a name when the above code is compiled and executed. You are supposed to enter a value and then hit enter to see the following result −

Tell me your name: Rajshree Verma

Your good name is: Rajshree Verma 

The C++ compiler always determines the data type of the entered value and it selects the appropriate stream extraction operator which will be used to extract the value and store it in the given variables.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more