Is it possible for printf to display subscripts and superscripts? For example, if you need to display on the screen just such an entry 
|
2 answers
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₂)² - 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
- 2The main message of the answer was that in the utf-8 source, the characters themselves represent the more readable equivalent of the
\uxxxxsequence. - vp_arth
|
char[]is a sequence of bytes that can represent text in any encoding. - jfsprintffunction does not "display anything" on the screen. Theprintffunction 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