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 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;
}
// without using namespace
#include <iostream>
int main(){
std:: cout << "Hi everyone" ;
return 0;
}
Now ,you can run the code even without writing return 0;
code will run well.Not necessary to write.
Both gives the same output.
Now imagine you wanna print. For example :
Hi everyone !!
My name is Aiera .
You can use as many cout
needed to print.
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:
OR
It is okay to use stream insertion operator (<<)
and place the \n
character after the text.
OR
Using endline character endl
And all the three ways gives same 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 {}
Top comments (0)