5

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?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
system
  • 81
  • 1
  • 2
  • 4
  • 2
    Wait, who in this world is still using MS-DOS? –  May 29 '11 at 15:11
  • Recalling old memoirs, I remember we had to do a setting in DOS itself to change the "code page". Don't remember - it's been a long time since I coded in DOS. – itsols May 29 '11 at 15:16
  • @Radek me for example, developing on an embedded device for car diagnosis (legacy stuff anyway, but still in heavy use by our customers). – Federico klez Culloca May 29 '11 at 15:16
  • @Radek it's homework actually.. – system May 29 '11 at 15:25
  • 1
    I suspect the question is not about MS-DOS but running in a console on Windows. – Michael Burr May 29 '11 at 15:28
  • 1
    @Michael you're right. Now that I think of it I don't know why my book says MS-DOS in the first place. – system May 29 '11 at 15:51
  • 3
    Please edit the question to correctly specify if you mean legacy MS-DOS or a recent windows dos box/console. – Chris Stratton May 29 '11 at 15:55
  • To reinforce what Chris says, it's important to know which because the answer is *different* in the two cases; there have been profound changes in this area over the years. – Donal Fellows May 29 '11 at 15:56
  • Possible duplicate of [How to Output Unicode Strings on the Windows Console](https://stackoverflow.com/questions/3130979/how-to-output-unicode-strings-on-the-windows-console) – phuclv May 28 '17 at 04:05
  • [Output unicode strings in Windows console app](https://stackoverflow.com/q/2492077/995714), [Output Unicode to console Using C++, in Windows](https://stackoverflow.com/q/2849010/995714)... – phuclv May 28 '17 at 04:06

4 Answers4

4

Assuming Windows, you can:

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
McDowell
  • 107,573
  • 31
  • 204
  • 267
0

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.

Davislor
  • 14,674
  • 2
  • 34
  • 49
0

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.

Community
  • 1
  • 1
tofutim
  • 22,664
  • 20
  • 87
  • 148
0

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

printf("\u0220\n");

this will print Ƞ

eyadof
  • 318
  • 3
  • 12