Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C++ Tutorial

C++ HOME C++ Intro C++ Get Started C++ Syntax C++ Output C++ Comments C++ Variables C++ User Input C++ Data Types C++ Operators C++ Strings C++ Math C++ Booleans C++ If...Else C++ Switch C++ While Loop C++ For Loop C++ Break/Continue C++ Arrays C++ Structures C++ Enums C++ References C++ Pointers

C++ Functions

C++ Functions C++ Function Parameters C++ Function Overloading C++ Scope C++ Recursion

C++ Classes

C++ OOP C++ Classes/Objects C++ Class Methods C++ Constructors C++ Access Specifiers C++ Encapsulation C++ Inheritance C++ Polymorphism C++ Files C++ Exceptions C++ Date

C++ Data Structures

C++ Data Structures & STL C++ Vectors C++ List C++ Stacks C++ Queues C++ Deque C++ Sets C++ Maps C++ Iterators C++ Algorithms

C++ How To

C++ Add Two Numbers C++ Random Numbers

C++ Reference

C++ Reference C++ Keywords C++ <iostream> C++ <fstream> C++ <cmath> C++ <string> C++ <cstring> C++ <ctime>

C++ Examples

C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Certificate


C++ cout object

❮ iostream objects


Example

Use the cout object to output different data types:

string myStr = "Hello World!";
bool myBool = false;
int myInt = 50;
float myFloat = 19.99;

cout << myStr << "\n";
cout << myBool << "\n";
cout << myInt << "\n";
cout << myFloat << "\n";

Try it Yourself »


Definition and Usage

The cout object is used to output values/print text.

The most common way to use cout is with the << insertion operator. The insertion operator decides how to represent a variable or literal value based on its data type.

cout << "Hello World!";

Try it Yourself »

The insertion operator can be used more than once on the same line to output multiple values:

cout << "The answer is: " << x;

Try it Yourself »

Note: The cout object is defined in the <iostream> header file.


Manipulators

Manipulators allow you to change the formatting of the output. They are used with the << insertion operator in the same way as literal values and variables, and they affect output that follows them.

Except for setw(), the effect of a manipulator remains until another another manipulator changes it.

The table below shows a list of useful manipulators:

Manipulator Description Example
boolalpha Displays boolean values as "true" and "false" instead of "1" and "0". cout << boolalpha << false;
dec Represents integers as decimal digits. cout << dec << 12;
endl Outputs a newline character. This manipulator also flushes the output buffer which makes it less efficient than printing \n. cout << "Line 1" << endl << "Line 2";
ends Outputs the null terminating character used to end C-style strings. Mainly used when writing into files. cout << "Hello World!" << ends;
fixed Represents floating point numbers with a fixed number of decimal places. The number of decimal places can be established with the setprecision() manipulator. cout << fixed << 19.99;
hex Represents integers as hexadecimal digits. cout << hex << 12;
internal If a width is specified (using the setw() manipulator), numbers will have their sign left-aligned while the value is right-aligned, other data types will have the output aligned to the right. cout << setw(10) << internal << -12345;
left If a width is specified (using the setw() manipulator), aligns output to the left. cout << setw(10) << left << "Hello";
noboolalpha Used to reset the change made by the boolalpha manipulator. cout << noboolalpha << false;
noshowbase Used to reset the change made by the showbase manipulator. cout << hex << noshowbase << 12;
noshowpoint Used to reset the change made by the showpoint manipulator. cout << noshowpoint << 12345.0;
noshowpos Used to reset the change made by the showpos manipulator. cout << noshowpos << 12;
nouppercase Used to reset the change made by the uppercase manipulator. cout << hex << nouppercase << 12;
oct Represents integers as octal digits. cout << oct << 12;
right If a width is specified (using the setw() manipulator), aligns output to the right. cout << setw(10) << right << "Hello";
fixed Represents floating point numbers in scientific notation. The number of decimal places can be established with the setprecision() manipulator. cout << fixed << 19.99;
setfill() Chooses a character to use as padding.
Requires the <iomanip> library.
cout << setfill('.') << setw(10) << 19.99;
setprecision() Chooses the precision of floating point numbers. If the fixed or scientific manipulators were used it specifies the number of decimal places, otherwise it specifies the number of significant digits.
Requires the <iomanip> library.
cout << setprecision(4) << 12.3456;
setw() Specifies the minimum number of characters wide the next output should be. If the output is not wide enough then padding is added to fill up the remaining space.
Requires the <iomanip> library.
cout << setw(10) << "Hello";
showbase When representing integers as hexadecimal or octal, prefixes the numbers with "0x" or "0" to show their base. cout << hex << showbase << 12;
showpoint Always displays the decimal point for floating point numbers even if it is not needed. cout << showpoint << 12345.0;
showpos Always displays a + sign next to positive numbers. cout << showpos << 12;
uppercase Represents hexadecimal digits and the scientific notation "e" in uppercase. cout << hex << uppercase << 12;

Example

Use manipulators to change how output is formatted:

// Booleans
cout << "Booleans\n";
cout << false << "\n";
cout << boolalpha << false << "\n";

// Hexadecimal and octal numbers
cout << "\nHexadecimal and octal numbers\n";
int myInt = 14;
cout << dec << myInt << "\n";
cout << hex << myInt << "\n";
cout << oct << myInt << "\n";
cout << showbase << uppercase;
cout << hex << myInt << "\n";
cout << oct << myInt << "\n";
cout << dec;

// Floating point numbers
cout << "\nFloating point numbers\n";
float myFloat = 19.99;
cout << myFloat << "\n";
cout << showpos << showpoint << 12345.0 << "\n";
cout << noshowpos << noshowpoint;
cout << fixed << myFloat << "\n";
cout << scientific << myFloat << "\n";

// Alignment
cout << "\nAlignment\n";
cout << setw(10) << left << "Left" << "\n";
cout << setw(10) << right << "Right" << "\n";
cout << setw(10) << internal << -12345 << " (Internal)\n";

Try it Yourself »


Methods

The cout object also has methods which can do the same operations as the << insertion operator.

Output methods

The cout.write(str, n) method outputs the first n characters from the char array str without any formatting.

Example

char myStr[] = "Hello World!";
cout.write(myStr, 5);

Try it Yourself »

The cout.put(c) method outputs the specified character c without any formatting.

Example

char grade = 'B';
cout.put(grade);

Try it Yourself »

Formatting methods

The cout.precision(p) method specifies how many digits are used to represent floating point numbers. By default it specifies the number of significant digits to display. If the ios::fixed or ios::scientific flag is enabled then it specifies how many digits follow the decimal point.

Example

cout.precision(4);
cout << 12.3456;

Try it Yourself »

The cout.width(w) method specifies the minimum number of characters wide the next output should occupy. If the output does not have enough characters then padding characters will be added to fill up the remaining space. By default the padding characters are spaces and they are added to the left so that the content is aligned to the right. The alignment can be changed by using one of the ios::adjustfield flags described in the Flags section below.

Example

cout.width(10);
cout << 5 << "\n";
cout.width(10);
cout << 25 << "\n";
cout.width(10);
cout << 125 << "\n";

Try it Yourself »

The cout.fill(c) method specifies which character will be used as padding.

Example

cout.fill('.');
cout.width(10);
cout << 5 << "\n";
cout.width(10);
cout << 25 << "\n";
cout.width(10);
cout << 125 << "\n";

Try it Yourself »

Flags

The setf() and unsetf() methods are used to set or unset flags which change the formatting of the output. There are a variety of different flags. Some flags belong to a group and, in that case, the setf() method should have a second parameter specifying which group it belongs to so that the other flags of the group can be reset.

Flag Syntax Description
ios::boolalpha cout.setf(ios::boolalpha) Displays boolean values as "true" and "false" instead of "1" and "0".
ios::showbase cout.setf(ios::showbase) When representing integers as hexadecimal or octal, prefixes the numbers with "0x" or "0" to show their base.
ios::showpoint cout.setf(ios::showpoint) Always displays the decimal point for floating point numbers even if it is not needed.
ios::showpos cout.setf(ios::showpoint) Always displays a + sign next to positive numbers.
ios::uppercase cout.setf(ios::uppercase) Represents hexadecimal digits and the scientific notation "e" in uppercase.
ios::dec cout.setf(ios::dec, ios::basefield) Represents integers as decimal digits. Belongs to the ios::basefield group.
ios::hex cout.setf(ios::hex, ios::basefield) Represents integers as hexadecimal digits. Belongs to the ios::basefield group.
ios::oct cout.setf(ios::oct, ios::basefield) Represents integers as octal digits. Belongs to the ios::basefield group.
ios::fixed cout.setf(ios::fixed, ios::floatfield) Represents floating point numbers with a fixed number of decimal places. The number of decimal places can be established with the cout.precision() method. Belongs to the ios::floatfield group.
ios::scientific cout.setf(ios::scientific, ios::floatfield) Represents floating point numbers in scientific notation. The number of decimal places can be established with the cout.precision() method. Belongs to the ios::floatfield group.
ios::internal cout.setf(ios::internal, ios::adjustfield) If a width is specified, for numbers the sign is left-aligned while the value is right-aligned, for other data types the output is aligned to the right. Belongs to the ios::adjustfield group.
ios::left cout.setf(ios::left, ios::adjustfield) Aligns output to the left when a width is specified. Belongs to the ios::adjustfield group.
ios::right cout.setf(ios::right, ios::adjustfield) Aligns output to the right when a width is specified. Belongs to the ios::adjustfield group.

Example

Use flags to change how output is formatted:

// Booleans
cout << "Booleans\n";
cout << false << "\n";
cout.setf(ios::boolalpha);
cout << false << "\n";

// Hexadecimal and octal numbers
cout << "\nHexadecimal and octal numbers\n";
int myInt = 14;
cout << myInt << "\n";
cout.setf(ios::hex, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::oct, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::showbase);
cout.setf(ios::uppercase);
cout.setf(ios::hex, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::oct, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::dec, ios::basefield);

// Floating point numbers
cout << "\nFloating point numbers\n";
float myFloat = 19.99;
cout << myFloat << "\n";
cout.setf(ios::fixed, ios::floatfield);
cout << myFloat << "\n";
cout.setf(ios::scientific, ios::floatfield);
cout << myFloat << "\n";
cout.unsetf(ios::floatfield);
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout << 12345.0 << "\n";

// Alignment
cout << "\nAlignment\n";
cout.setf(ios::left, ios::adjustfield);
cout << setw(10) << "Left" << "\n";
cout.setf(ios::right, ios::adjustfield);
cout << setw(10) << "Right" << "\n";
cout.setf(ios::internal, ios::adjustfield);
cout << setw(10) << 12345.0 << " (Internal)\n";

Try it Yourself »


❮ iostream objects

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.