Greek letters in Windows Concole

2019-06-23 08:50发布

问题:

I'm writting a program in C and I want to have Greek characters in the menu when I run it in cmd.exe . Someone said that in order to include Greek characters you have to use a printf that goes something like this:

 printf(charset:IS0-1089:uffe);      

but they weren't sure.

Does anyone know how to do that?

回答1:

Assuming Windows, you can:

  • set your console font to a Unicode TrueType font:
  • emit the data using an "ANSI" mechanism

This code prints γειά σου:

#include "windows.h"

int main() {
  SetConsoleOutputCP(1253); //"ANSI" Greek
  printf("\xE3\xE5\xE9\xDC \xF3\xEF\xF5");
  return 0;
}

The hex codes represent γειά σου when encoded as windows-1253. If you use an editor that saves data as windows-1253, you can use literals instead. An alternative would be to use either OEM 737 (that really is a DOS encoding) or use Unicode.

I used SetConsoleOutputCP to set the console code page, but you could type the command chcp 1253 prior to running the program instead.



回答2:

you can print a unicode char characters by using printf like this :

printf("\u0220\n");

this will print Ƞ



回答3:

I think this might only work if your console supports Greek. Probably what you want to do is to map characters to the Greek, but using ASCII. For C# but same idea in C.

913 to 936 = upper case Greek letters

945 to 968 = lower case Greek letters

Read more at Suite101: Working with the Greek Alphabet and C#: How to Display ASCII Codes Correctly when Creating a C# Application | Suite101.com at this link.



回答4:

One way to do this is to print a wide string. Unfortunately, Windows needs a bit of non-standard setup to make this work. This code does that setup inside #if blocks.

#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>

/* This has been reported not to autodetect correctly on tdm-gcc. */
#ifndef MS_STDLIB_BUGS // Allow overriding the autodetection.
#  if ( _WIN32 || _WIN64 )
#    define MS_STDLIB_BUGS 1
#  else
#    define MS_STDLIB_BUGS 0
#  endif
#endif

#if MS_STDLIB_BUGS
#  include <io.h>
#  include <fcntl.h>
#endif

void init_locale(void)
// Does magic so that wprintf() can work.
{
  // Constant for fwide().
  static const int wide_oriented = 1;

#if MS_STDLIB_BUGS
  // Windows needs a little non-standard magic.
  static const char locale_name[] = ".1200";
  _setmode( _fileno(stdout), _O_WTEXT );
#else
  // The correct locale name may vary by OS, e.g., "en_US.utf8".
  static const char locale_name[] = "";
#endif

  setlocale( LC_ALL, locale_name );
  fwide( stdout, wide_oriented );
}

int main(void)
{
  init_locale();
  wprintf(L"μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽\n");
  return EXIT_SUCCESS;
}

This has to be saved as UTF-8 with a BOM in order for older versions of Visual Studio to read it properly. Your console also has to be set to a monospaced Unicode font, such as Lucida Console, to display it properly. To mix wide strings in with ASCII strings, the standard defines the %ls and %lc format specifiers to printf(), although I’ve found these don’t work everywhere.

An alternative is to set the console to UTF-8 mode (On Windows, do this with chcp 65001.) and then print the UTF-8 string with printf(u8"μουσάων Ἑλικωνιάδων ἀρχώμεθ᾽\n");. UTF-8 is a second-class citizen on Windows, but that usually works. Try to run that without setting the code page first, though, and you will get garbage.