C++ Print Numbers
In this page:
std::cout << number << std::endl;
std::cout << number1 operator number2 << std::endl;
Printing Numeric Literals
cout << can print numeric literals directly, without any conversion, and will display the number exactly as written, whether it's a whole integer or a decimal value.
Example: Printing Numeric Literals
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << 42 << std::endl;
// Print to the console with cout
std::cout << 3.14 << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing Arithmetic Results
When cout is given a mathematical expression, C++ evaluates that expression completely first and only then prints the single resulting number, not the original expression text.
Example: Printing Arithmetic Results
#include <iostream>
int main() {
std::cout << 5 + 3 << std::endl; // prints 8, not "5 + 3"
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing Different Number Types
cout << works with every numeric type in C++ -- short, int, long, float, and double -- and prints each one formatted according to that type's own conventions.
Example: Printing Different Number Types
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare s and set it to 10
short s = 10;
// Declare i and set it to 1000
int i = 1000;
long l = 100000L;
float f = 1.5f;
double d = 3.14159;
// Print to the console with cout
std::cout << s << " " << i << " " << l << " " << f << " " << d << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Concatenating Numbers with Text
The << operator can be chained multiple times in a single cout statement, mixing string literals and numeric values, and any number combined this way is automatically converted to its printable text form.
Example: Concatenating Numbers with Text
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
int score = 95;
// Print to the console with cout
std::cout << "Score: " << score << " out of 100" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Formatting Numbers in Output
For more control over how a number appears, the iomanip library provides manipulators like setprecision and fixed for a fixed number of decimal places, and setw for a minimum field width.
Example: Formatting Numbers in Output
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.5
double price = 19.5;
// Print to the console with cout
std::cout << std::fixed << std::setprecision(2) << price << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Expecting
cout << 5 / 2to print 2.5, when both are ints so it prints 2. - Writing
cout << "Total: " + 5;, which does pointer arithmetic on the string instead of joining text and a number. - Forgetting that
cout << 10 + 5 << 3prints 153 as separate values, rather than adding everything together.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: