Writing C90 compatible code

J

jacob navia

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
 
B

Ben Pfaff

jacob navia said:
(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 (...));
 
N

Nick

jacob navia said:
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.
 
B

Ben Pfaff

Nick said:
jacob navia said:
(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.
 
J

jacob navia

Ben Pfaff a écrit :
#include <limits.h>

#ifdef LLONG_MAX
...
#endif


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

That looks quite reasonable. Thanks.
 
N

Nick

Nick said:
jacob navia said:
(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.
 
N

Nobody

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

Keith Thompson

Nobody said:
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.
 
F

FredK

Keith Thompson said:
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.
 
T

Tom St Denis

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
 
S

sandeep

jacob said:
Ben Pfaff a écrit :

That looks quite reasonable. Thanks.

I think it will give a wrong answer when
size_of(size_t)>size_of(long unsigned int)
however....
 
T

Tim Rentsch

Keith Thompson said:
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 <[email protected]> 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.)
 
K

Keith Thompson

Tim Rentsch said:
It seems to me that the posting from <[email protected]> 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.
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top