Cout in C++:
In previous tutorial, we learnt how to store data. Now we
will learn in this C++ tutorial that how to print that desired result on the
console.
In C++, output on the standard output device is accomplished
through the use of "cout" and the operator “ << ”.
Note:
The standard output device is usually the screen.Syntax:
cout << expression or identifier ;
This is called an output statement. In C++, "<<" is called the stream insertion operator.
Example:
Here are some simple c programs with output to show the use of this command.
1. char ch = A.
cout << ch;
Output:
A //This A was stored in char
variable ch
2. cout << ‘A’;
Output:
A //This A was hard coded
For char ch variable, you didn’t needed double quotes but for hard coded ‘A’ you needed double quotes.
Statement Output
cout <<
29 / 4 << endl; 7
cout
<<“Hello World”<< endl; Hello
World
cout
<<20<< endl; 20
cout
<<3.2<< endl; 3.2
cout <<
”29 / 4“ << endl; 29
/ 4
cout
<<20 + 100<< endl; 120
cout
<<‘A’<< endl; A
cout
<<“4 + 16 =”<<4 + 20 <<endl; 4
+ 16 = 20
cout
<<2 + 3 * 4<< endl; 14
cout <<“Hello
\nWorld”<< endl; Hello
World
Look at the
statement cout << “Hello \nWorld”
<< endl;
This
statement contains \n. It is a newline character. It causes the insertion point
to move to the beginning of the next line before printing there. That’s why it
prints Hello one line and World on the next line.
Note:In
C++, \ is called the escape character and \n is called newline escape sequence.
If we don’t
assign any value to a variable and try to cout it on the screen then it will
print a junk value.
Example:
int num;
cout <<
num <<endl;
The output
will be some junk value.
0 comments:
Post a Comment