Is it possible for printf to display subscripts and superscripts? For example, if you need to display on the screen just such an entry enter image description here

  • It depends on which visualization tool you use. In Unicode 2 - superscript for example exists - you can simply output it. In ANSI char [] no. - nick_n_a
  • @nick_n_a char[] is a sequence of bytes that can represent text in any encoding. - jfs
  • one
    First of all, the terminal should be ready for this - vp_arth
  • 2
    The printf function does not "display anything" on the screen. The printf function only writes a sequence of characters to a standard text output stream. And in what form this stream will appear before your eyes (if it appears at all) depends only on the properties and settings of the display tool through which you will view the contents of this stream. It is from this that one should make a start. - AnT
  • 2
    You can use printf to print HTML or any LaTeX and on the go display any suitable program: D - andreymal

2 answers 2

If the terminal is configured for utf-8 encoding and the font supports the corresponding characters:

 #include <stdio.h> int main(void) { printf(u8"(X\u2081 - X\u2082)\u00b2 + (Y\u2081 - Y\u2082)\u00b2\n"); } 

Example:

 $ gcc -std=c11 *.c && ./a.out (X₁ - X₂)² + (Y₁ - Y₂)² 

On Windows, wprintf() can be used . Here is a portable solution in C ++ .

  • Beautiful :) I did not expect it myself :) - nick_n_a

In the utf-8 terminal, you can even do this:

 #include <stdio.h> #include <wchar.h> #include <locale.h> int main() { setlocale(LC_ALL, ""); printf("(X₁ - X₂)²+(Y₁ - Y₂)²\n"); // Или printf(u8"(X₁ - X₂)²+(Y₁ - Y₂)²\n"); с -std=c11 return 0; } 

Conclusion:

 sh-4.2$ gcc -o main *.c sh-4.2$ main (X₁ - X₂)²+(Y₁ - Y₂)² 

Feeddle

  • There is no guarantee that the compiler will save bytes in utf-8: Is literal necessary in C ++ 11 - jfs
  • That never had a problem with it. MB, due to the lack of a zoo compiler? But, take note) - vp_arth
  • 2
    The main message of the answer was that in the utf-8 source, the characters themselves represent the more readable equivalent of the \uxxxx sequence. - vp_arth