How to determine (in compile time) whether a compiler supports wide characters or not

A

Ashabul Yeameen

Hi all,
I am writing a C program which at some certain steps needs to use the
wide character funcion fputwc() for giving utf-8 output. Since I want
to make the code more portable, I wrote my own fputwc() function. But
I want to use my own funcion in the case where the compiler does not
supply this funcion. Is there any way, i.e. any preprocessor
directive, to know whether the supports wide character or not.

-Yeameen
 
K

Keith Thompson

Thomas Matthews said:
Perhaps:
#ifdef fputwc
#endif
????

Nope. The fputwc function may be implemented as a macro, but it
doesn't have to be. The #ifdef directive can't tell you whether a
function is defined, and there's no portable compile-time method for
determining whether a given function or type is defined.

As far as the standard is concerned, the wchar_t type isn't optional,
any more than size_t or int. It must be defined in <stddef.h>. This
is true both for C90 and for C99. If you're dealing with a compiler
that doesn't fully support either the C90 or the C99 standard, it's
likely to be missing other important features as well -- such as
prototypes. There are ways of writing code that will work under such
old implementations; it's up to you to decide whether it's worth the
effort. (I think gcc is still designed to be compiled with pre-C89
compilers; you might look at the gcc sources for ideas.)

You may also need to worry about whehter fputwc() is going to give you
utf-8 output, even if it exists.

Probably your best approach is to use some small test programs to tell
you, by whether or not they compile, what features are supported. Try
compiling the test programs as part of your configuration process.
During configuration, you might generate your own C headers that you
include in your own code. (I think the GNU autoconf system does this
kind of thing; if you want to know more about that, you'll need to
read its documentation or find an appropriate newsgroup.)
 
A

Ashabul Yeameen

Keith Thompson said:
Nope. The fputwc function may be implemented as a macro, but it
doesn't have to be. The #ifdef directive can't tell you whether a
function is defined, and there's no portable compile-time method for
determining whether a given function or type is defined.

As far as the standard is concerned, the wchar_t type isn't optional,
any more than size_t or int. It must be defined in <stddef.h>. This
is true both for C90 and for C99. If you're dealing with a compiler
that doesn't fully support either the C90 or the C99 standard, it's
likely to be missing other important features as well -- such as
prototypes. There are ways of writing code that will work under such
old implementations; it's up to you to decide whether it's worth the
effort. (I think gcc is still designed to be compiled with pre-C89
compilers; you might look at the gcc sources for ideas.)

You may also need to worry about whehter fputwc() is going to give you
utf-8 output, even if it exists.

Yes, it is true. One of the two new compilers (which support wchar_t)
gave me utf-8 output. Probably it is dependent on the enviroment. btw,
I decided to use the follwing funcion (i found on the net) for getting
utf-8 output always.

-Yeameen

void fputwidec(int uni, FILE *fp)
{
if (uni < 0x80) {
fputc(uni, fp);
}
else if (uni < 0x800) {
fputc(0xC0 | uni>>6, fp);
fputc(0x80 | uni & 0x3F, fp);
}
else if (uni < 0x10000) {
fputc(0xE0 | uni>>12, fp);
fputc(0x80 | uni>>6 & 0x3F, fp);
fputc(0x80 | uni & 0x3F, fp);
}
}
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top