Day 2: C++ language

Please refer Introduction to C++ before you proceed .

Topics to be covered

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

How to Print in C++

The programming ins…


This content originally appeared on DEV Community and was authored by Mehfila A Parkkulthil

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;
}

using namespace

// without using namespace 

#include <iostream>
int main(){

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

}

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 .

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++


This content originally appeared on DEV Community and was authored by Mehfila A Parkkulthil


Print Share Comment Cite Upload Translate Updates
APA

Mehfila A Parkkulthil | Sciencx (2025-01-02T03:17:27+00:00) Day 2: C++ language. Retrieved from https://www.scien.cx/2025/01/02/day-2-c-language/

MLA
" » Day 2: C++ language." Mehfila A Parkkulthil | Sciencx - Thursday January 2, 2025, https://www.scien.cx/2025/01/02/day-2-c-language/
HARVARD
Mehfila A Parkkulthil | Sciencx Thursday January 2, 2025 » Day 2: C++ language., viewed ,<https://www.scien.cx/2025/01/02/day-2-c-language/>
VANCOUVER
Mehfila A Parkkulthil | Sciencx - » Day 2: C++ language. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/02/day-2-c-language/
CHICAGO
" » Day 2: C++ language." Mehfila A Parkkulthil | Sciencx - Accessed . https://www.scien.cx/2025/01/02/day-2-c-language/
IEEE
" » Day 2: C++ language." Mehfila A Parkkulthil | Sciencx [Online]. Available: https://www.scien.cx/2025/01/02/day-2-c-language/. [Accessed: ]
rf:citation
» Day 2: C++ language | Mehfila A Parkkulthil | Sciencx | https://www.scien.cx/2025/01/02/day-2-c-language/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.