Header Ads

Header ADS

Cout,endl and escape

cout_endl_and_escape_sequences.pdf
What is
cout
cout
is used for printing output on the terminal/console screen.
CODE
#include<iostream>
using namespace std;
int main()
{
cout << "My first C++ program.";
}
OUTPUT
cout
can be used multiples times to print
multiples
words/sentences.
CODE
#include<iostream>
using namespace std;
int main()
{
cout << "
Whatever
";
cout << "
Whatever
";
cout << "
Whatever
";
}
OUTPUT
What is
endl
endl
is used for
ending a line
and creating a new line.
See the following
examples.
CODE
OUTPUT
#include<iostream>
using namespace std;
int main()
{
cout << "
Batman
"
<< endl
;
cout << "
Vs.
"
<< endl
;
cout << "
Shamsu
"
<< endl
;
}
#include<iostream>
using namespace std;
int main()
{
cout << "
What
"
<< endl
;
cout <<
endl
;
cout << "
the
"
<< endl
;
}
It is possible to use several
endl
’s
with a single
cout
as shown below.
CODE
#include<iostream>
using namespace std;
int main()
{
cout << "
Around
"
<< endl <<
"
the
"
<< endl <<
"
world
"
<< endl
;
}
OUTPUT
javedhossain.com/csc101
Escape sequences
\
n
is an escape sequence that
creates a new line. It
can be
used
instead of
endl
.
CODE
#include<iostream>
using namespace std;
int main()
{
cout << "This
\
n
is
\
n
ridiculous!
";
}
OUTPUT
CODE
#include<iostream>
using namespace std;
int main()
{
cout << "* * * * *
\
n
";
cout << "* *
\
n
";
cout << "* *
\
n
";
cout << "* *
\
n
";
cout << "* * * * *
\
n
";
}
OUTPUT
javedhossain.com/c1
Following are some useful escape sequences.
Try them out.
Escape
sequence
Character represented
\
a
Beep
(sound)
\
b
Backspace
\
n
Newline
\
t
Horizontal Tab
\
v
Vertical Tab
\
\
Backslash
\
'
Single quotation mark
\
"
Double
quotation mark
Following is an example showing how to print a slash (
\
), a single quote ( ' ) and a double quote ( " ).
CODE
#include<iostream>
using namespace std;
int
main()
{
cout << "Slash:
\
\
"
<< endl
;
cout << "Single quote:
\
'
"
<< endl
;
cout << "Double quote:
\
"
"
<< endl
;
}
OUTPUT
 / 4

No comments

Powered by Blogger.