Basic input and output in C++

In this C++ guide we will learn about basic input and output  in C++. Actually C++ uses stream concept for input and output which is the sequence of bytes. Stream can make the performance faster. When bytes or data flows from main memory to output device then it is called output operation. When bytes or data flows from input devices to main memory then it is called input operation.

About C++ output

The cout object is used to show output in C++. It is used together with insertion operator ( << ) to show output on the screen.

#include <iostream>
using namespace std;

int main(){
    cout << "I am learning C++ input/output now.";
    return 0;
}

This will print the string after insertion operator which is inside the inverted comma (” “). We can also add a number of insertion operator under one cout object like bellow.

#include <iostream>
using namespace std;

int main( ){
   char myarr[] = "This is C++ input/output tutorial";

   cout << "Text before : " << myarr << " : Text after" << endl;
   return 0;
}

Program output:

basic-input-output-in-c-c-output-stream-c-input-stream

endl is used to print a new line on the output screen here. You can also use \n to print a new line as like as C programming.

We have used standard namespace here which will make our program more readable. However we can avoid using standard namespace which makes the program more efficient. Then we have to use std::cout instead of cout object.

#include <iostream>

int main(){
  std::cout << "This is C++ input/output tutorial.\n";
  
  return 0;
}

You may have noticed that we have used \n to print new line here instead of endl. However, you can skip using new line if want.

We can also print any number or character using cout object. The program bellow is using cout to print the number and character.

#include <iostream>
using namespace std;

int main(){
    int firstNum = 5;
    float secondNum = 7.75;
    char mych = 'x';

    cout << "First Number is = " << firstNum << endl;     // printing the integer
    cout << "Second Number is = " << secondNum << endl;   // printing the double
    cout << "Sum is = " << firstNum + secondNum << endl;  // printing the sum
    cout << "Entered character is = " << mych << endl;    // printing the char

    return 0;
}

Output of this C++ program:

First Number is = 5
Second Number is = 7.75
Sum is = 12.75
Entered character is = x

About C++ input

cin is the predefined object which is used to take input from the user. It is used with extraction operator ( >> ) to reads data.

#include <iostream>
using namespace std;

int main(){
    int number;

    cout << "Enter a number here : ";
    cin >> number;
    cout << "You have entered : " << number << endl;

    return 0;
}

This program will prompt the user to give any number as input and then store it to the variable ‘number’ and then print the value of given number like this.

Enter a number here : 55
You have entered : 55

We can take different input using cin object. Different input means multiple type of input. Let’s see the bellow program to take multiple input using cin object.

// C++ input and output program
#include <iostream>
using namespace std;

int main(){
    char c;
    int n;

    cout << "Enter any number and character here : ";
    cin >> n >> c;

    cout << "Entered number is : " << n << endl;
    cout << "Entered character is : " << c << endl;

    return 0;
}

Output:

Enter any number and character here : 40 b
Entered number is : 40
Entered character is : b

Note:

  • cout is pronounced as “see out” and cin is pronounced as “see in”.
  • ( << ) is called insertion operator where ( >> ) is known as extraction operator.
  • endl and \n both can be used to print a new line in C++.
  • All C code is valid in C++ (except a few) but no C++ code is valid in C.

Previous pageFirst C++ program

Next page:  C++ comments