NO

Author Topic: Wrong german Umlauts in a consoleprogramm  (Read 8938 times)

kurzer

  • Guest
Wrong german Umlauts in a consoleprogramm
« on: October 22, 2009, 09:44:10 PM »
Hello everybody,

I'm really new to this forum and to the programming language C.
While working through a C beginners tutorial I stumbled over a small issue, which I can't solve (and it's annoying me ;) ).

The german Umlauts (special characters) are not correctly displayed in the windows console using the printf() command.
When I start the programm it prints other chars instead of the german Umlauts (see picture)

Code: [Select]
#include <stdio.h>

int main(void) {

   printf("Österreichische Äpfel findet man überall - auch unter Fußmatten\n");
// Oesterreichische Aepfel findet man ueberall, auch unter Fussmatten
   return 0;
}

The standard codepage in my Console is 850, but also changing to 1252 does not change the problem.
Can someone explain me this behavior, please.

Thank you in advance.

[Windows XP 32 Bit, SP2, same on SP3]
« Last Edit: October 22, 2009, 10:02:37 PM by kurzer »

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Wrong german Umlauts in a consoleprogramm
« Reply #1 on: October 22, 2009, 09:52:59 PM »
You will have to use Unicode strings to get the correct output.

Sorry, no example currently available, but searching the forum should get you a hit.
---
Stefan

Proud member of the UltraDefrag Development Team

kurzer

  • Guest
Re: Wrong german Umlauts in a consoleprogramm
« Reply #2 on: October 22, 2009, 10:50:01 PM »
Thanks for your fast reply, Stefan.
Did you mean wchars and wprint?

Quote
#include <stdio.h>
#include <wchar.h>

int main(void)
{
   wchar_t wstr[] = L"öäüß";
   wprintf(L"Umlaute: %ls\n", wstr);

   return 0;
}

This code also prints the wrong umlauts.

Maybe I got you wrong and searched for the wrong attempt?
I know very well what you mean with UNICODE, but I can't assign it to the right C functions.

But never mind. It's a satisfiable information for me, if you say "It's a normal behavior for 'fprint()', don't worry"
I'm really at the beginning of the C tutorial and I hope this Unicode-stuff will be explained in one of the the following chapters.

I was simply irritated, because every compiled example of the tutorial prints broken Umlauts. And I thought: "Damn, why the author of this tutorial does not explain the broken Umlauts? He otherwise explained every little sideeffect in his examples."
btw: It's a german tutorial (openbook) - "C von A bis Z" and the author use Umlauts in his printf() functions.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Wrong german Umlauts in a consoleprogramm
« Reply #3 on: October 23, 2009, 07:33:27 AM »
Test this code:
Code: [Select]
#include <stdio.h>

int main(void)
{
   char strANSI[] = "öäüß";
   char strOEM[] = "„”á";

/*
   strOEM[0] = 0x94; //148
   strOEM[1] = 0x84; //132
   strOEM[2] = 0x81; // 129
   strOEM[3] = 0xE1; // 225
*/
   printf("Umlaute: OEM: [%s] ANSI: [%s]\n", strOEM, strANSI);

   return 0;
}
May the source be with you

kurzer

  • Guest
Re: Wrong german Umlauts in a consoleprogramm
« Reply #4 on: October 23, 2009, 10:39:57 PM »
Hi timovjl,

thanks for your examplecode.
The 'cryptic' OEM codes looks like right Umlauts in the console output (see picture).

Hmm, that means each time, I want to write a program with console outputs, I have to enter this special chars instead of Umlauts. :-\ Not very handy.

I never wrote programs for console mode. But after running your code I tried this using my favorit language (PureBasic) and was suprised.
Same problem with console prints using the Basic compiler.  :o

It seems this 'problem' is not a problem. Maybe it's caused because the sourcecode editor running in Windows mode use another codemap as the console.

Okay, now this is claryfied for me.

Thanks guys.

Edit: PS: Ahhh! Now I tried it successfully with the CharToOEM() WinAPI function using PureBasic (see Umlaut3.gif).
I guess it's a similar way in C.  :)
« Last Edit: October 23, 2009, 10:50:31 PM by kurzer »

Greg

  • Guest
Re: Wrong german Umlauts in a consoleprogramm
« Reply #5 on: October 24, 2009, 05:07:56 AM »
Quote
Maybe it's caused because the sourcecode editor running in Windows mode use another codemap as the console.

That is correct.

As you have found, different fonts and different code pages give different results.

Take a look at the Character Map (charmap.exe) Windows accessory.  And look up "Code Pages" in MSDN.
 

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Wrong german Umlauts in a consoleprogramm
« Reply #6 on: October 24, 2009, 08:48:11 AM »
I was simply irritated, because every compiled example of the tutorial prints broken Umlauts. And I thought: "Damn, why the author of this tutorial does not explain the broken Umlauts? He otherwise explained every little sideeffect in his examples."
btw: It's a german tutorial (openbook) - "C von A bis Z" and the author use Umlauts in his printf() functions.

There is currently a discussion about this book taking place at the Use-Net in the group de.comp.lang.c, thread C-Buch für Lau, which tells us that the book includes many misinformation, but it is not the worst available.

So I would work my way through this book with caution.
---
Stefan

Proud member of the UltraDefrag Development Team

Offline nsp

  • Member
  • *
  • Posts: 15
Re: Wrong german Umlauts in a consoleprogramm
« Reply #7 on: October 24, 2009, 08:55:01 AM »
Hello everybody,

I'm really new to this forum and to the programming language C.
While working through a C beginners tutorial I stumbled over a small issue, which I can't solve (and it's annoying me ;) ).

If you want, you can use windows api to output your text according to the codepage. The following function do, it for you !

Code: [Select]
#include <windows.h>
#include <strsafe.h>

unsigned long int win_wprintf(LPCWSTR szFormat, ...)
{
#       ifndef ARRAY_SIZE
#         define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#       endif

wchar_t   szText[1024 * 10];
va_list args;
unsigned long int   dwWritten;
 
/* Format the text into the buffer */
va_start(args, szFormat);
StringCbVPrintfW(szText, ARRAY_SIZE(szText), szFormat, args);
va_end(args);
 
/* Output the buffer to the console. */
if (WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), szText, lstrlenW(szText), &dwWritten, NULL))
{
return dwWritten;
}

return 0;
}


int main(void) {

   win_wprintf(L"Österreichische Äpfel findet man überall - auch unter Fußmatten\n");
// Oesterreichische Aepfel findet man ueberall, auch unter Fussmatten
   return 0;
}

you need to compile with windows extension on !

You can also use the conversion function CharToOemBuffW that convert from any string to oem (you need to link with user32.lib)
« Last Edit: October 24, 2009, 09:53:08 AM by nsp »

kurzer

  • Guest
Re: Wrong german Umlauts in a consoleprogramm
« Reply #8 on: October 26, 2009, 10:04:49 AM »
Thank you very much for your comments, guys. *thumbsup*

@Stefan Pendl: Thank you for the link to de.comp.lang.c (C-Buch für Lau).

I have read the thread and I think that the discussed issues/failures are not to critical for me. Most of posts matters about the name and concept of datatypes (byte, char and so on). Some of them seems slightly wrong explained by the autor. But most of the definitions I know from my days on the good old Amiga, which I programmed in Assembler (16 years ago). And I do not plan to write C programms for esoteric machines using 9 or 18 Bits for a Byte. ;)

So, I will go on reading the book and keep your warnings in mind.

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Wrong german Umlauts in a consoleprogramm
« Reply #9 on: January 27, 2010, 07:32:06 AM »
Hello everybody,

I'm really new to this forum and to the programming language C.
While working through a C beginners tutorial I stumbled over a small issue, which I can't solve (and it's annoying me ;) ).

The german Umlauts (special characters) are not correctly displayed in the windows console using the printf() command.
When I start the programm it prints other chars instead of the german Umlauts (see picture)


I use in this cases an add-in (I don't know from the Pelles C homepage or from John's homepage) which can convert ANSI to OEM charaters.

If I use this add-in your code will get to:
Code: [Select]
#include <stdio.h>

int main(void) {

   printf("™sterreichische Žpfel findet man berall - auch unter Fuámatten\n");
// Oesterreichische Aepfel findet man ueberall, auch unter Fussmatten
   return 0;
}
looks funny but gives the correct output.

PS.: Schaut lustig aus, aber das Ergebnis passt. ;)
best regards
 Alex ;)

cd

  • Guest
Re: Wrong german Umlauts in a consoleprogramm
« Reply #10 on: January 27, 2010, 07:25:39 PM »
I was simply irritated, because every compiled example of the tutorial prints broken Umlauts. And I thought: "Damn, why the author of this tutorial does not explain the broken Umlauts? He otherwise explained every little sideeffect in his examples."
btw: It's a german tutorial (openbook) - "C von A bis Z" and the author use Umlauts in his printf() functions.
There is currently a discussion about this book taking place at the Use-Net in the group de.comp.lang.c, thread C-Buch für Lau, which tells us that the book includes many misinformation, but it is not the worst available.
So I would work my way through this book with caution.
the openbook was the second book i took for starting with C , but the discussion about the sizeof(x);  shouldn't irretate our shorty("kurzer"). for sure is, that the openbook got a couple of mistakes/errors wich don't make the book bad, if you wanna fix those mistakes its always a good thing to have a second book with it, where it is already displayed wright. Even Addision-Wesley's C Book got an error inside wich turns out when you pay attention to the questions and the practice part ;).
« Last Edit: February 05, 2010, 08:23:05 PM by cd »