DEV Community

Mehfila A Parkkulthil
Mehfila A Parkkulthil

Posted on • Edited on

Day 2: C++ language - Output

Please refer Introduction to C++ before you proceed .


Topics to be covered

  1. How to Print in C++ (or to get output in c++).
  2. How to add a new line character (Escape sequence).
  3. Comments

How to Print in C++

  • The programming instruction we give are called statements.
  • The statements are executed one by one , in the order they are written.
  • The compiler ignores all the white space.
  • Everything that needs to be executed should be written inside curly {}brackets.

To print any form of text or numbers or to get output we use cout (pronounced as see-out).

For Example:

  • Imagine we want to print "Hi everyone" .
  • Any long form of text(ie,string) that has to printed should be written in quoatation marks.

There are two ways to print one using namespace std other without .

using namespace std: You are informing the compiler that you are going to use the std (standard) namespace, so you don't need to prefix standard library entities with std::. And both works well .

// Using namespace

#include <iostream>
using namespace std;
int main(){

cout << " Hi everyone" ;
return 0;
}
Enter fullscreen mode Exit fullscreen mode

using namespace

// without using namespace 

#include <iostream>
int main(){

std:: cout << "Hi everyone" ;
return 0;

}
Enter fullscreen mode Exit fullscreen mode

without using namespace
Now ,you can run the code even without writing return 0;code will run well.Not necessary to write.

Both gives the same output.

output

Now imagine you wanna print. For example :

Hi everyone !!
My name is Aiera .
Enter fullscreen mode Exit fullscreen mode

You can use as many cout needed to print.

Multiple lines

output of multiple line

Now you might have noticed even though I have mentioned my cout's in 2 different my output that has been printed on single line. To get the output in new line we need to add a newline character.


How to add a newline character.

Newline chararcter or escape sequence \n or endl .

To create a new line you can use any of these (\n or endl ) in different ways .

\n is used to insert a newline.

For example:

newline

OR

It is okay to use stream insertion operator (<<) and place the \ncharacter after the text.

newline character

OR
Using endline character endl

endl

And all the three ways gives same output.

newline output


Comments

These are statements that are visible to us . They are not executed by the compiler and are intended to helpreaders understand the code.They wont affect the code or output.

There are two types of comments in C++:

  • Single-line comments: These begin with // and continue to the end of the line.
  • Multi-line comments: These begin with /* and end with */. They can cover multiple lines.

You can write your comments anywhere you want not necessarily inside your curlybrackets {}

single line comment

multiple line comment


Previous Blog

Introduction to C++

Top comments (0)