Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Writing C90 compatible code (http://www.velocityreviews.com/forums/t725450-writing-c90-compatible-code.html)

jacob navia 06-13-2010 04:34 PM

Writing C90 compatible code
 
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

Ben Pfaff 06-13-2010 04:41 PM

Re: Writing C90 compatible code
 
jacob navia <jacob@spamsink.net> 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_

Nick 06-13-2010 04:47 PM

Re: Writing C90 compatible code
 
jacob navia <jacob@spamsink.net> 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

Ben Pfaff 06-13-2010 05:01 PM

Re: Writing C90 compatible code
 
Nick <3-nospam@temporary-address.org.uk> writes:

> jacob navia <jacob@spamsink.net> 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.

jacob navia 06-13-2010 05:55 PM

Re: Writing C90 compatible code
 
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.

Nick 06-13-2010 06:08 PM

Re: Writing C90 compatible code
 
blp@cs.stanford.edu (Ben Pfaff) writes:

> Nick <3-nospam@temporary-address.org.uk> writes:
>
>> jacob navia <jacob@spamsink.net> 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

Nobody 06-13-2010 06:16 PM

Re: Writing C90 compatible code
 
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.


Keith Thompson 06-13-2010 07:54 PM

Re: Writing C90 compatible code
 
Nobody <nobody@nowhere.com> 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) kst-u@mib.org <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"

FredK 06-13-2010 11:09 PM

Re: Writing C90 compatible code
 

"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnbpbe3dyu.fsf@nuthaus.mib.org...
> Nobody <nobody@nowhere.com> 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.




Tom St Denis 06-14-2010 11:51 AM

Re: Writing C90 compatible code
 
On Jun 13, 7:09*pm, "FredK" <fred.nos...@dec.com> wrote:
> "Keith Thompson" <ks...@mib.org> wrote in message
>
> news:lnbpbe3dyu.fsf@nuthaus.mib.org...
>
> > Nobody <nob...@nowhere.com> 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


All times are GMT. The time now is 07:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.