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

Discussion in 'C Programming' started by Ashabul Yeameen, Sep 16, 2003.

  1. 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
     
    Ashabul Yeameen, Sep 16, 2003
    #1
    1. Advertising

  2. Thomas Matthews <> writes:
    > Ashabul Yeameen wrote:
    > > 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

    >
    > 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.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Sep 17, 2003
    #2
    1. Advertising

  3. Keith Thompson <> wrote in message news:<>...
    > Thomas Matthews <> writes:
    > > Ashabul Yeameen wrote:
    > > > 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

    > >
    > > 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.


    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);
    }
    }



    >
    > 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.)
     
    Ashabul Yeameen, Sep 17, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web Developer

    char 8bit wide or 7bit wide in c++?

    Web Developer, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    591
    John Harrison
    Jul 31, 2003
  2. Ashabul Yeameen
    Replies:
    1
    Views:
    349
    Mike Wahler
    Sep 16, 2003
  3. Howard Gardner
    Replies:
    17
    Views:
    528
    Howard Gardner
    Apr 25, 2006
  4. Marcin Kalicinski
    Replies:
    1
    Views:
    290
    kwikius
    Aug 3, 2007
  5. er ci
    Replies:
    12
    Views:
    1,866
Loading...

Share This Page