Writing C90 compatible code

Discussion in 'C Programming' started by jacob navia, Jun 13, 2010.

  1. jacob navia

    jacob navia Guest

    I am releasing the first version of the container library this week.
    I have 2 problems to solve, where I would appreciate your help

    (1): long long doesn't exist in C90. Is there a way of circumventing
    that? How could I detect at compile time if long long is supported?
    In any way I can always publish the code so that C90 works in 32 bits
    only. People that need 64 bits would use a 64 bit compiler.

    (2) The"%z" specifier doesn't exist in C90. Is there any work around?

    My command line under UNIX is:

    gcc -g -Wno-pointer-sign -DUNIX -Wall -pedantic -c -o foo.o foo.c


    Thanks
     
    jacob navia, Jun 13, 2010
    #1
    1. Advertising

  2. jacob navia

    Ben Pfaff Guest

    jacob navia <> writes:

    > (1): long long doesn't exist in C90. Is there a way of circumventing
    > that? How could I detect at compile time if long long is supported?
    > In any way I can always publish the code so that C90 works in 32 bits
    > only. People that need 64 bits would use a 64 bit compiler.


    #include <limits.h>

    #ifdef LLONG_MAX
    ....
    #endif

    > (2) The"%z" specifier doesn't exist in C90. Is there any work around?


    printf("%lu", (unsigned long) sizeof (...));
    --
    "Programmers have the right to be ignorant of many details of your code
    and still make reasonable changes."
    --Kernighan and Plauger, _Software Tools_
     
    Ben Pfaff, Jun 13, 2010
    #2
    1. Advertising

  3. jacob navia

    Nick Guest

    jacob navia <> writes:

    > I am releasing the first version of the container library this week.
    > I have 2 problems to solve, where I would appreciate your help
    >
    > (1): long long doesn't exist in C90. Is there a way of circumventing
    > that? How could I detect at compile time if long long is supported?
    > In any way I can always publish the code so that C90 works in 32 bits
    > only. People that need 64 bits would use a 64 bit compiler.


    Don't confuse "long long" and 64 bits. It really is possible for
    something to be 64 bits long and not a long long. Or a long. Or even,
    IIRC, an int.

    I'd use a bit of pre-compile build magic with a very small program to
    see if "long long" can compile (the way autoconf does things), and
    probably printing of some sizeofs, then generate an appropriate header
    file that gets included by each part.

    > (2) The"%z" specifier doesn't exist in C90. Is there any work around?


    The usual thing to do is to use ul and a cast for good measure. It
    won't work if you have - say - 32 bit longs and a single object bigger
    than that of course. Again, though, you can see if you have ull in your
    pre-compilation jiggery-pokery.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
     
    Nick, Jun 13, 2010
    #3
  4. jacob navia

    Ben Pfaff Guest

    Nick <> writes:

    > jacob navia <> writes:
    >> (2) The"%z" specifier doesn't exist in C90. Is there any work around?

    >
    > The usual thing to do is to use ul and a cast for good measure. It
    > won't work if you have - say - 32 bit longs and a single object bigger
    > than that of course. [...]


    It doesn't make much sense for a C90 implementation to have
    32-bit longs but objects bigger than 2^32-1 bytes.
    --
    Just another C hacker.
     
    Ben Pfaff, Jun 13, 2010
    #4
  5. jacob navia

    jacob navia Guest

    Ben Pfaff a écrit :
    >
    > #include <limits.h>
    >
    > #ifdef LLONG_MAX
    > ...
    > #endif
    >
    >> (2) The"%z" specifier doesn't exist in C90. Is there any work around?

    >
    > printf("%lu", (unsigned long) sizeof (...));


    That looks quite reasonable. Thanks.
     
    jacob navia, Jun 13, 2010
    #5
  6. jacob navia

    Nick Guest

    (Ben Pfaff) writes:

    > Nick <> writes:
    >
    >> jacob navia <> writes:
    >>> (2) The"%z" specifier doesn't exist in C90. Is there any work around?

    >>
    >> The usual thing to do is to use ul and a cast for good measure. It
    >> won't work if you have - say - 32 bit longs and a single object bigger
    >> than that of course. [...]

    >
    > It doesn't make much sense for a C90 implementation to have
    > 32-bit longs but objects bigger than 2^32-1 bytes.


    You are, of course, right.

    OTOH, I bet there are non-standard dialects out there that had long long
    but no %z qualifier. On those size_t could be an unsigned long long and
    they could support such large objects.

    I'm pretty sure long long was codifying existing practice.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
     
    Nick, Jun 13, 2010
    #6
  7. jacob navia

    Nobody Guest

    On Sun, 13 Jun 2010 10:01:47 -0700, Ben Pfaff wrote:

    > It doesn't make much sense for a C90 implementation to have
    > 32-bit longs but objects bigger than 2^32-1 bytes.


    Tell that to Microsoft. Win64 has a 32-bit "long" for compatibility with
    Win32.
     
    Nobody, Jun 13, 2010
    #7
  8. Nobody <> writes:
    > On Sun, 13 Jun 2010 10:01:47 -0700, Ben Pfaff wrote:
    >
    >> It doesn't make much sense for a C90 implementation to have
    >> 32-bit longs but objects bigger than 2^32-1 bytes.

    >
    > Tell that to Microsoft. Win64 has a 32-bit "long" for compatibility with
    > Win32.


    Ok.

    Hey, Microsoft, it doesn't make much sense for a C90 implementation to
    have 32-bit longs but objects bigger than 2^32-1 bytes.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 13, 2010
    #8
  9. jacob navia

    FredK Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Nobody <> writes:
    >> On Sun, 13 Jun 2010 10:01:47 -0700, Ben Pfaff wrote:
    >>
    >>> It doesn't make much sense for a C90 implementation to have
    >>> 32-bit longs but objects bigger than 2^32-1 bytes.

    >>
    >> Tell that to Microsoft. Win64 has a 32-bit "long" for compatibility with
    >> Win32.

    >
    > Ok.
    >
    > Hey, Microsoft, it doesn't make much sense for a C90 implementation to
    > have 32-bit longs but objects bigger than 2^32-1 bytes.
    >


    Nonsense, from a code compatability standpoint, it makes perfect sense - and
    MS is not alone in this regard.
     
    FredK, Jun 14, 2010
    #9
  10. jacob navia

    Tom St Denis Guest

    On Jun 13, 7:09 pm, "FredK" <> wrote:
    > "Keith Thompson" <> wrote in message
    >
    > news:...
    >
    > > Nobody <> writes:
    > >> On Sun, 13 Jun 2010 10:01:47 -0700, Ben Pfaff wrote:

    >
    > >>> It doesn't make much sense for a C90 implementation to have
    > >>> 32-bit longs but objects bigger than 2^32-1 bytes.

    >
    > >> Tell that to Microsoft. Win64 has a 32-bit "long" for compatibility with
    > >> Win32.

    >
    > > Ok.

    >
    > > Hey, Microsoft, it doesn't make much sense for a C90 implementation to
    > > have 32-bit longs but objects bigger than 2^32-1 bytes.

    >
    > Nonsense, from a code compatability standpoint, it makes perfect sense - and
    > MS is not alone in this regard.


    /rant

    Only if they were compatible with a) their own spec and b) a real
    standard ...

    Tom
     
    Tom St Denis, Jun 14, 2010
    #10
  11. jacob navia

    sandeep Guest

    jacob navia writes:
    > Ben Pfaff a écrit :
    >>
    >> #include <limits.h>
    >>
    >> #ifdef LLONG_MAX
    >> ...
    >> #endif
    >>
    >>> (2) The"%z" specifier doesn't exist in C90. Is there any work around?

    >>
    >> printf("%lu", (unsigned long) sizeof (...));

    >
    > That looks quite reasonable. Thanks.


    I think it will give a wrong answer when
    size_of(size_t)>size_of(long unsigned int)
    however....
     
    sandeep, Jun 18, 2010
    #11
  12. jacob navia

    Tim Rentsch Guest

    Keith Thompson <> writes:

    > Nobody <> writes:
    >> On Sun, 13 Jun 2010 10:01:47 -0700, Ben Pfaff wrote:
    >>
    >>> It doesn't make much sense for a C90 implementation to have
    >>> 32-bit longs but objects bigger than 2^32-1 bytes.

    >>
    >> Tell that to Microsoft. Win64 has a 32-bit "long" for compatibility with
    >> Win32.

    >
    > Ok.
    >
    > Hey, Microsoft, it doesn't make much sense for a C90 implementation to
    > have 32-bit longs but objects bigger than 2^32-1 bytes.


    It seems to me that the posting from <> was
    topical, relevant, and at least mildly interesting. Why
    poke fun at it? (Perhaps the comment was just meant to be
    funny and I've missed the humor. Needless to say that's
    never happened to me before.)
     
    Tim Rentsch, Jun 20, 2010
    #12
  13. Tim Rentsch <> writes:
    > Keith Thompson <> writes:
    >> Nobody <> writes:
    >>> On Sun, 13 Jun 2010 10:01:47 -0700, Ben Pfaff wrote:
    >>>> It doesn't make much sense for a C90 implementation to have
    >>>> 32-bit longs but objects bigger than 2^32-1 bytes.
    >>>
    >>> Tell that to Microsoft. Win64 has a 32-bit "long" for compatibility with
    >>> Win32.

    >>
    >> Ok.
    >>
    >> Hey, Microsoft, it doesn't make much sense for a C90 implementation to
    >> have 32-bit longs but objects bigger than 2^32-1 bytes.

    >
    > It seems to me that the posting from <> was
    > topical, relevant, and at least mildly interesting.


    Agreed.

    > Why
    > poke fun at it?


    Just for the sake of poking fun at it.

    > (Perhaps the comment was just meant to be
    > funny and I've missed the humor. Needless to say that's
    > never happened to me before.)


    I know, that never happens to me either.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 20, 2010
    #13
    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. Steven D'Aprano

    Writing backwards compatible code

    Steven D'Aprano, Apr 14, 2006, in forum: Python
    Replies:
    14
    Views:
    591
    Laurent Pointal
    Apr 24, 2006
  2. Bernard

    Is these functions C90 and/or C99 ?

    Bernard, Jun 23, 2004, in forum: C Programming
    Replies:
    3
    Views:
    1,232
    E. Robert Tisdale
    Jun 24, 2004
  3. Jason Curl

    Char difference between C90 and C99

    Jason Curl, Jun 21, 2005, in forum: C Programming
    Replies:
    11
    Views:
    797
  4. printf()-like macro in C90

    , Nov 9, 2005, in forum: C Programming
    Replies:
    6
    Views:
    362
    Zoran Cutura
    Nov 14, 2005
  5. pantagruel
    Replies:
    0
    Views:
    246
    pantagruel
    Feb 17, 2006
Loading...

Share This Page