wchar_t problem

J

Jan Engelhardt

Hello ng,


I have found that the following program only prints "empty" but not "hello
world". Does anybody know why this happens?

#include <stdio.h>
#include <wchar.h>
int main(void) {
wprintf(L"empty\n");
printf("hello world\n");
return 0;
}

For reference, I have tried gcc 3.3.5/glibc 2.3.4 and gcc 4.0.2/glibc 2.3.5.
Both have this weirdness. All LC_* variables are "POSIX".


Jan
--
 
E

Emmanuel Delahaye

Jan Engelhardt wrote on 21/08/05 :
I have found that the following program only prints "empty" but not "hello
world". Does anybody know why this happens?

#include <stdio.h>
#include <wchar.h>
int main(void) {
wprintf(L"empty\n");
printf("hello world\n");
return 0;
}

Sounds good to me.
For reference, I have tried gcc 3.3.5/glibc 2.3.4 and gcc 4.0.2/glibc 2.3.5.
Both have this weirdness. All LC_* variables are "POSIX".

Works fine for me on Windows 98SE with mingw :

C:\DEV-CPP\BIN>gcc --version
GCC.EXE (GCC) 3.4.2 (mingw-special)
<...>


The LC_ things are unchanged (default value)


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
R

Robert Gamble

Jan said:
Hello ng,


I have found that the following program only prints "empty" but not "hello
world". Does anybody know why this happens?

#include <stdio.h>
#include <wchar.h>
int main(void) {
wprintf(L"empty\n");

By calling wprintf on the (unoriented) stdout stream you have made the
stream become a wide-oriented stream.
printf("hello world\n");

You are calling a byte-oriented output function on a (now)
wide-oriented stream, this is undefined behavior. For files you can
use freopen to reset the stream orientation, you cannot change the
orientation of the stdout stream after it has been set though. Try
something like this instead:

printf("%ls", L"empty\n");
printf("hello world\n");

Robert Gamble
 
J

Jan Engelhardt

Works fine for me on Windows 98SE with mingw :

Under 98SE with gcc 3.2.3 and also MSVC++6 CL (Command line compiler),
it works too. But that does not tell me why it's not working under Linux.
No special chars (ascii < 32) are printed, because redirecting stdout does not
show any special chars (which it should under unices, in case there were any).
If someone got has {diet,uc,k}libc (if with wprintf support)
I'd be happy to hear back if it happens there too.


Jan
--
 
J

Jan Engelhardt

By calling wprintf on the (unoriented) stdout stream you have made the
stream become a wide-oriented stream.

Thanks, this actually made me to RTFM and found a reference to the fwide()
function. After all, I will be writing just a few lines to a file, so if I had
not tried printing mixed stuff to stdout, I'd probably never have noticed it.
You are calling a byte-oriented output function on a (now)
wide-oriented stream, this is undefined behavior.

So, according to the standard, I am allowed to start wide operations on a
char-oriented stream while I can't go back?


Jan Engelhardt
--
 
J

Jan Engelhardt

So, according to the standard, I am allowed to start wide operations on a
char-oriented stream while I can't go back?

Nevermind, the doc says it all. The given example actually worked 50% because
of the "no orientation yet" state.



Jan Engelhardt
--
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top