views:

116

answers:

3

I'd like to have my program output "cm2" (cm squared).

How do make a superscript 2?

+5  A: 

This is not something C++ can do on its own.

You would need to use a specific feature of your console system.

I am not aware of any consoles or terminals that implement super-script. I might be wrong though.

Zan Lynx
+1  A: 

Yes, I agree with Zan.

Basic C++ does not have any inbuilt functionality to print superscripts or subscripts. You need to use any additional UI library.

krissam
Technically speaking, C++ doesn't have functionality for outputting ANY specific character. Even for basic alphanumeric characters like 'a' or '2' C++ has no functionality to actually output them. All it is capable of doing is converting those literals into character codes that are then interpreted by the output stream they are sent to. Generally this results in seeing an 'a' or a '2' on the screen but given some weird console and/or font system you could get anything.
Noah Roberts
+12  A: 

As Zan said, it depends what character encoding your standard output supports. If it supports Unicode , you can use the encoding for ²(U+00B2). If it supports the same Unicode encoding for source files and standard output, you can just embed it in the file. For example, my GNU/Linux system uses UTF-8 for both, so this works fine:

#include <iostream>

int main()
{
    std::cout << "cm²" << std::endl;
}
Matthew Flaschen
Ah! Clever. I never thought of Unicode.
Zan Lynx
Doesn't work for just Unicode. On ISO-8859-1, ² is `char(0xB2)`. Since my Windows system uses that for source files and console output, I too can embed it directly in my source files.
MSalters