Using cout in C++

By: Baski Emailed: 1606 times Printed: 2075 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

To print a value to the screen, write the word cout, followed by the insertion operator (<<), which you create by typing the less-than character (<) twice. Even though this is two characters, C++ treats it as one.

Follow the insertion character with your data. Listing below illustrates how this is used. Type in the example exactly as written, except substitute your own name where you see Baski (unless your name is Baski, in which case leave it just the way it is; it's perfect).

Using cout.

1:     // Listing 2.2 using cout
2:
3:     #include <iostream.h>
4:     int main()
5:     {
6:        cout << "Hello there.\n";
7:        cout << "Here is 5: " << 5 << "\n";
8:        cout << "The manipulator endl writes a new line to the screen." <<  
                      Âendl;
9:        cout << "Here is a very big number:\t" << 70000 << endl;
10:       cout << "Here is the sum of 8 and 5:\t" << 8+5 << endl;
11:       cout << "Here's a fraction:\t\t" << (float) 5/8 << endl;
12:       cout << "And a very very big number:\t" << (double) 7000 * 7000 << 
                      Âendl;
13:       cout << "Don't forget to replace Baski with your name...\n";
14:       cout << "Baski is a C++ programmer!\n";
15:        return 0;
16: }

Hello there.
Here is 5: 5
The manipulator endl writes a new line to the screen.
Here is a very big number:      70000
Here is the sum of 8 and 5:     13
Here's a fraction:              0.625
And a very very big number:     4.9e+07
Don't forget to replace Baski with your name...
Baski is a C++ programmer!

On line 3, the statement #include <iostream.h> causes the iostream.h file to be added to your source code. This is required if you use cout and its related functions.

On line 6 is the simplest use of cout, printing a string or series of characters. The symbol \n is a special formatting character. It tells cout to print a newline character to the screen.

Three values are passed to cout on line 7, and each value is separated by the insertion operator. The first value is the string "Here is 5: ". Note the space after the colon. The space is part of the string. Next, the value 5 is passed to the insertion operator and the newline character (always in double quotes or single quotes). This causes the line

Here is 5: 5

to be printed to the screen. Because there is no newline character after the first string, the next value is printed immediately afterwards. This is called concatenating the two values.

On line 8, an informative message is printed, and then the manipulator endl is used. The purpose of endl is to write a new line to the screen. (Other uses for endl are discussed on Day 16.)

On line 9, a new formatting character, \t, is introduced. This inserts a tab character and is used on lines 8-12 to line up the output. Line 9 shows that not only integers, but long integers as well can be printed. Line 10 demonstrates that cout will do simple addition. The value of 8+5 is passed to cout, but 13 is printed.

On line 11, the value 5/8 is inserted into cout. The term (float) tells cout that you want this value evaluated as a decimal equivalent, and so a fraction is printed. On line 12 the value 7000 * 7000 is given to cout, and the term (double) is used to tell cout that you want this to be printed using scientific notation. All of this will be explained on Day 3, "Variables and Constants," when data types are discussed.

On line 14, you substituted your name, and the output confirmed that you are indeed a C++ programmer. It must be true, because the computer said so!


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(8)


1. View Comment

Great tutorial. Thanks guys.

View Tutorial          By: John D at 2009-03-21 11:01:58
2. View Comment

i want to say only one thing

great explain


View Tutorial          By: ashish at 2009-06-09 21:36:45
3. View Comment

It would be nice to add the line,

using namespace std;

to your example. Tried gnu compiler which inforces the namespace requirement.


View Tutorial          By: Anonymous at 2009-08-30 07:59:10
4. View Comment

Good example. However as someone said, it would be great if you could add "using namespace std;" after #include...
Thanks.

Neo


View Tutorial          By: Neo Cambell at 2010-03-10 11:02:06
5. View Comment

sir needed help i am compiler dev c++ which showning error about
accepting cout


View Tutorial          By: shahab at 2010-05-05 13:17:53
6. View Comment

fantastic tutorial and very well explained, I've only just started to teach myself C++ and found this very helpful.

View Tutorial          By: Francis at 2011-08-14 19:57:18
7. View Comment

cout<<5+"object orinted programming

View Tutorial          By: SANDEEP SHARMA at 2011-09-18 19:06:57
8. View Comment

Line 11 says:
cout << "Here's a fraction:\t\t" << (float) 5/8 << endl;

You explained:
On line 11, the value 5/8 is inserted into cout. The term (float) tells cout that you want this value evaluated as a decimal equivalent, and so a fraction is printed.

But I think (float) tells the compiler to cast the integer '5' as a float before performing the evaluation of 5/8: 5.0/8 The 8 remains an int, but it gets promoted to float during the evaluation, so the result is a float. cout then deals with the result, which is a float.

Wrapping 5/8 in parens causes the fraction to be evaluated and that result cast to a float. 5/8 is zero. The zero is cast to a float and printed.

cout << "Here's a fraction:\t\t" << (float) (5/8) << endl;


View Tutorial          By: Nrom at 2012-06-26 20:16:52

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Using cout in C++
assert() Versus Exceptions in C++
Using peek() and putback() with cin in C++
Program to add two numbers in C++
A Brief History of C++
Difference between Procedural, Structured, and Object-Oriented Programming
C++ and Object-Oriented Programming
Getting Started with C++
Using Comments in a C++ Program
Using functions in C++
How to use Enumerated constants in C++
Demonstration of Prefix and Postfix operators in C++
The if Statement in C++
Advanced if Statements in C++
Use of Conditional (Ternary) Operator in C++
Most Emailed Articles (in C++)
C++ and Object-Oriented Programming
Constructors and Destructors in C++
Pass by Reference in C++ functions
Vector variables in C++
The if Statement in C++
Nested Loops in C++
Operator Precedence in C++
strcat() and strncat() sample program in C++
Getting User Input Using cin in C++
Escape Sequences for Nonprintable Characters in C++
Classes with Other Classes as Member Data in C++
The Stack and the Free Store in C++
Dot (.) vs Arrow (->) to access data members in C++
Converting Pointer Parameters in C++ to Java
qsort() sample program in C++