printf for french chars

K

kaustubh.deo

I am facing issues printing french chars like [ é ] using printf
function.
I have reproduced this issue with simple C program as follows.


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

int main(int argc, char **argv)
{
if (argc > 1)
{
printf("%s\n", argv[1]);
}
return 0;
}

execute this prog from dos prompt passing string say "abc_éèçàù"
where chars following '_' are french chars.
I get a garbled output on command promt. Where as if I debug this
prog(using MS studio), I can see that argv[1] has the proper string.

additional info:
1> default console page is 850
2> Running Windows in french locale.

Does any body why does it happens.
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

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

int main(int argc, char **argv)
{
if (argc > 1)
{
printf("%s\n", argv[1]);
}
return 0;
}

no prob on macos x :

~/work/C/essais/locale%> ycc test french.c
~/work/C/essais/locale%> ./test éèà
éèà
~/work/C/essais/locale%>
 
M

Michael Mair

I am facing issues printing french chars like [ é ] using printf
function.
I have reproduced this issue with simple C program as follows.


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

int main(int argc, char **argv)
{
if (argc > 1)
{
printf("%s\n", argv[1]);
}
return 0;
}

execute this prog from dos prompt passing string say "abc_éèçàù"
where chars following '_' are french chars.
I get a garbled output on command promt. Where as if I debug this
prog(using MS studio), I can see that argv[1] has the proper string.

additional info:
1> default console page is 850
2> Running Windows in french locale.

Does any body why does it happens.

Why did you not set the locale for your C programme?
You start in the "C" locale where you are only guaranteed to have
access to the characters from the source character set.
Try setting the locale using setlocale().

Cheers
Michael
 
K

kaustubh.deo

I had had a go at setting locale.
but the question is, input to program from console seems to work as
argv has a valid string in it. point to note is tht these chars are
from code page 850 which is default.
The only issue is in displaying it on console.

thanks,
kaustubh

Michael said:
I am facing issues printing french chars like [ é ] using printf
function.
I have reproduced this issue with simple C program as follows.


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

int main(int argc, char **argv)
{
if (argc > 1)
{
printf("%s\n", argv[1]);
}
return 0;
}

execute this prog from dos prompt passing string say "abc_éèçàù"
where chars following '_' are french chars.
I get a garbled output on command promt. Where as if I debug this
prog(using MS studio), I can see that argv[1] has the proper string.

additional info:
1> default console page is 850
2> Running Windows in french locale.

Does any body why does it happens.

Why did you not set the locale for your C programme?
You start in the "C" locale where you are only guaranteed to have
access to the characters from the source character set.
Try setting the locale using setlocale().

Cheers
Michael
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

Michael Mair said:
Why did you not set the locale for your C programme?
You start in the "C" locale where you are only guaranteed to have
access to the characters from the source character set.
Try setting the locale using setlocale().

this "locale" isn't an environment variable ?
 
M

Michael Mair

Une said:
this "locale" isn't an environment variable ?

No; every C programme starts out in the "C" locale, see for example
C99, 7.11.1.1#4
"At program startup, the equivalent of
setlocale(LC_ALL, "C");
is executed."
What the "C" locale encompasses is partially implementation defined,
so you may in theory end up with a locale which "cannot even print
'$'", let alone accented characters.

Cheers
Michael
 
M

Michael Mair

Please do not top-post.

Michael said:
I am facing issues printing french chars like [ é ] using printf
function.
I have reproduced this issue with simple C program as follows.

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

int main(int argc, char **argv)
{
if (argc > 1)
{
printf("%s\n", argv[1]);
}
return 0;
}

execute this prog from dos prompt passing string say "abc_éèçàù"
where chars following '_' are french chars.
I get a garbled output on command promt. Where as if I debug this
prog(using MS studio), I can see that argv[1] has the proper string.

additional info:
1> default console page is 850
2> Running Windows in french locale.

Does any body why does it happens.

Why did you not set the locale for your C programme?
You start in the "C" locale where you are only guaranteed to have
access to the characters from the source character set.
Try setting the locale using setlocale().
>
I had had a go at setting locale.

With which effect?
but the question is, input to program from console seems to work as
argv has a valid string in it.

Note that what you see in the debugger is not necessarily what the
programme "sees". The debugger might display character values according
to a "code page" different from the one that the programme uses.
point to note is tht these chars are
from code page 850 which is default.
The only issue is in displaying it on console.

Read my other reply, <[email protected]>.
The initial locale always is "C".


-Michael
 
?

=?ISO-8859-1?Q?Une_b=E9vue?=

Michael Mair said:
No; every C programme starts out in the "C" locale, see for example
C99, 7.11.1.1#4
"At program startup, the equivalent of
setlocale(LC_ALL, "C");
is executed."
What the "C" locale encompasses is partially implementation defined,
so you may in theory end up with a locale which "cannot even print
'$'", let alone accented characters.

OK, thanxs !
 
J

Jack Klein

I am facing issues printing french chars like [ é ] using printf
function.
I have reproduced this issue with simple C program as follows.


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

int main(int argc, char **argv)
{
if (argc > 1)
{
printf("%s\n", argv[1]);
}
return 0;
}

execute this prog from dos prompt passing string say "abc_éèçàù"
where chars following '_' are french chars.
I get a garbled output on command promt. Where as if I debug this
prog(using MS studio), I can see that argv[1] has the proper string.

additional info:
1> default console page is 850
2> Running Windows in french locale.

Does any body why does it happens.

I see a lot of replies that trail off into nothing useful.

You are asking in the wrong place. The C language defines nothing at
all about the glyphs (visual symbols) that appear when characters are
sent to any device. This is completely a Windows issue.

Ask in a Windows group.
 
K

kaustubh.deo

This is an issue with console applications running only on Windows
environments where ANSI code page is not equal to OEM code page.
1. When strings or characters are printed to MSDOS console the
characters are interpreted using OEM code page.
2. Command line arguments passed to program are encoded using ANSI code
page in argv[].

Now for most european language installations of Windows, ANSI and OEM
code pages differ for a running application. So the characters when
printed to console are interpreted wrongly.
Solution to this problem is to convert to OEM string just before
displaying.
#include <stdio.h>
#include <locale.h>

int main(int argc, char **argv)
{ char correctDisplay[1024];
if (argc > 1)
{
printf("%s\n", argv[1]);
strcpy((char*)correctDisplay, argv[1]);
CharToOem((const char*)correctDisplay, (char*)array); /* MUST for
correct display of extended chars */
printf("correctDisplay = %s\n", correctDisplay);
Note that this issue is reproducible for extended chars in english
language as well.
For more info see
http://blogs.msdn.com/michkap/archive/2005/02/08/369197.aspx


regards,
Kaustubh



Jack said:
I am facing issues printing french chars like [ é ] using printf
function.
I have reproduced this issue with simple C program as follows.


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

int main(int argc, char **argv)
{
if (argc > 1)
{
printf("%s\n", argv[1]);
}
return 0;
}

execute this prog from dos prompt passing string say "abc_éèçàù"
where chars following '_' are french chars.
I get a garbled output on command promt. Where as if I debug this
prog(using MS studio), I can see that argv[1] has the proper string.

additional info:
1> default console page is 850
2> Running Windows in french locale.

Does any body why does it happens.

I see a lot of replies that trail off into nothing useful.

You are asking in the wrong place. The C language defines nothing at
all about the glyphs (visual symbols) that appear when characters are
sent to any device. This is completely a Windows issue.

Ask in a Windows group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top