Javatpoint Logo
Javatpoint Logo

What is include iostream in C++

To perform any input and output operations in C++, we need to use iostream header files. Without an <iostream> header file, we cannot take input from the user or print any output.

Syntax of <iostream> header file:

There are two types of streams in the iostream header file

1. Input Stream: To take any input from the user, we need to use cin, which belongs to the input stream

Syntax to use input stream:

When the cin is executed, the cursor will be stopped at the particular statement until the value is entered. The value entered will be stored in a variable.

2. Output Stream: To print the output, we use built-in functions in the cout output stream

Syntax for cout

When cout is executed, the value in the variable will be printed.

Different operations in iostream

1. Cin

Using cin, we can take input from the user and store the value in the variable. We need to use the cin keyword followed by >> and the variable name.

Syntax:

To use cin, we need to use #include <iostream.h> as cin belongs to this header file, and without this, an error will occur.

Example code for cin:

Output:

What is include iostream in C++

Explanation:

In the above code, we used cin to take the input, so to use cin, we included <iostream.h> header file. When the input is taken, the string input is stored in the name variable.

2. Cout

To print the output, we need to use the cout keyword, which belongs to the iostream header file. To use cout, we need to use the cout keyword followed by << and variable or the statement to print the output.

The syntax for using cout:

Example of using cout:

Output:

What is include iostream in C++

Explanation:

In the above code, we used a cout statement to print the statement. To use the cout statement, we need to include iostream.h header file. Once the cout is executed, statement or variable value will be printing the output. To print any statement, we need to use double quotes (" "), and to print a variable value; we need to use just the variable name without double quotes(" ")

3. Cerr

Cerr is used to print errors in C++, which is present in the iostream header file. If we need to print any error message in the code if any condition fails, then cerr is very helpful.

The syntax for cerr is:

Example:

Output:

What is include iostream in C++

Explanation:

In the above example, we used cin to take the input and check if the given number is even or not. If the given number is not even, we need to print an error that the given number is not an even number. We use the cerr keyword in the iostream header file to print this error.

4. Clog:

The clog is also used to print error messages, but unlike cerr, clog is buffered, which means the error message is stored in a buffer and then will be printed, but cerr is unbuffered and will not store the error message in the buffer. The clog also belongs to the iostream header file. As the clog is buffered, it will not show the error message immediately. The clog is preferred more than cerr when efficiency is more important.

The syntax for clog:

Example:

Output:

What is include iostream in C++

Explanation:

In the above code, we are printing an error. To print this error, we use cerr, which belongs to the iostream header file. Before printing the error, we store the error in a buffer.

Example: For all the input and output streams

Output:

What is include iostream in C++

Explanation: In the above code, we used all the input and output streams in the iostream.h header file.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA