Skip to main content

Building Blocks in C++

Print Output in C++

Just performing operation and storing values in different variables is incomplete untill we can see their value and or any change on our screens. We will also see how printing output help us to determine any logical error in our code.

We have used the cout << statement many times in our previous lessons. It is used to print output on the screen. Now , we will learn more about printing in detail.

Short Revision

Let's start with a short revision of the print statement.

#include <iostream>
using namespace std;

int main(){

  // print integer
  cout << 87 << endl;

  // print character
  cout << 'r' << endl;

  // print text or string
  cout << "Welcome to Learn Yards" << endl;

  // print a variable
  int number = 4;

  cout << number << endl;


  return 0;
}

Output:

87
r
Welcome to Learn Yards
4

Note: We have used

  • Variable name without double quotes to print variables.
  • Single quotes to print a character's literal
  • Double quotes to print a string or text literal

We can also print text or string and variables using a single print statement. Let's see it with an example :

#include<iostream>
using namespace std;

int main(){

  int marks = 40;

  cout << "Marks : " << marks << endl;

  return 0;
}

Output:

Marks : 40

Please grab your attention here, cout << "Marks : " part prints the string and << marks prints the value of the variable.

Here we are using a technique to print multiple things using a single cout statement by inserting << between each part to be printed.

Let's see one more example :

#include <iostream>
using namespace std;

int main(){

  cout << "Hello..! " << "I am learning " << "C++ from Learn Yards" << endl;

  return 0;
}

Output:

Hello ..! I am learning C++ from Learn Yards.

Here we used the same << operator to separate three strings.


Practice

Create a variable to store marks scored by a student and print it in the format given below.

I have scored 25 marks in my maths exam
Solution Code :
#include <iostream>
using namespace std;

int main() {
	
	int marks = 25;

	cout << "I have scored " << marks << " in my maths exam" << endl;

	return 0;
}

Quiz

What is the output of the following code?

#include <iostream>
using namespace std;

int main(){

  int marks = 45;
  cout << " Marks is " << marks;

  return 0;

}

Printing Multiple Variables Together

We will be using the same way we printed strings and variables together by using << operator.

Let's see with an example,

#include<iostream>
using namespace std;

int main(){

  int rollNo = 10;
  double avgScore = 78.65;

  // print rollNo and avgScore together
  cout << "Roll No is " << rollNo << " . Average score is " << avgScore; 
  

  return 0;
}

Output:

Roll No is 10 . Average score is 78.65

As you can see, we have successfully printed two variables together using the same print statement.

A small task for you to change the above code to get the desired output:

Roll No is 10
Average score is 78.65
Solution Code :
#include<iostream>
using namespace std;

int main(){

  int rollNo = 10;
  double avgScore = 78.65;

  // print rollNo and avgScore together
  cout << "Roll No is " << rollNo << endl << "Average score is " << avgScore; 
  

  return 0;
}

As you can see, we have used endl in the print statement to print them in new lines.


So far, we have used endl to print a new line. Apart from that, we can also use a special character \n to print a new line.

When C++ sees \n inside the string, it will print a new line instead of \n itself. For example,

#include <iostream>
using namespace std;
 
int main() {
    
    cout << "Hi \n \n Welcome to Learn Yards";
    
    return 0;
}

Output:

Hi

Welcome to Learn Yards

Consider an \n equivalent to a Enter key. Therefore first \n moves the cursor from Hi to a second line and then another \n moves it to the third line and then the rest string is printed.

We can use \n to format our code to make it look clean. For example, in the previous section, the output of our code looked like this:

Roll No is 10 . Average score is 78.65

Now, let's format this using \n.

#include<iostream>
using namespace std;

int main(){

  int rollNo = 10;
  double avgScore = 78.65;

  // print rollNo and avgScore together
  cout << "Roll No is " << rollNo << " \n Average score is " << avgScore; 
  

  return 0;
}

Output:

Roll No is 10 
Average score is 78.65

Quiz

What is the output of the following code?

#include <iostream>
using namespace std;

int main() {
    
    cout << "Amazing! \n Good going";
    
    return 0;
}

Some common mistakes

  1. Don't forget to get a semicolon after the end of every statement.
  2. C++ is a case-sensitive language so make sure the correct names of variables and functions are used with proper case.
  3. Sometimes as a beginner, we tend to miss << while printing so try to avoid it.

In the symphony of code, cout conducts the harmony of words, transforming algorithms into poetic expressions