How to print superscript 3 in C++
Unicode superscript 3, or ³ is \u00b3
in utf-16 and \xc2\xb3
in UTF-8.
Hence, this would work with cout
, assuming your console is UTF8.
#include <iostream>
int main()
{
std::cout << "\xc2\xb3" << std::endl;
return 0;
}
To set your console in UTF-8 mode, you can do it in a number of ways, each is OS dependent, if needed at all. On Windows, you can run chcp 65001
from the command prompt before invoking your code:
If you can barely make out ³ getting printed above, let's zoom in closer:
Alternatively, you can do this in code via a Windows API, SetConsoleOutputCP
SetConsoleOutputCP(65001);
So this works as well from a Windows program without having to do any environment changes before running the program.
#include <windows.h>
#include <iostream>
int main()
{
SetConsoleOutputCP(65001);
std::cout << "\xc2\xb3" << std::endl;
return 0;
}
If you prefix the expression with a 0
, the compiler considers this value to be represented in the octal radix. Values in octal radix must be in the range of [0,7]
. The value 9
in the 0179
expression is outside of the octal radix range.
Try the following solution to print superscript 3:
std::cout << "x\u00b3" << std::endl;
The result: x3
References
- How to print subscripts/superscripts on the screen in C++?