Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

miscellany

1 view
Skip to first unread message

rihad

unread,
Jul 18, 2001, 6:36:27 PM7/18/01
to
Hi there, I have some unsorted questions:

1) Here - http://www.ajk.tele.fi/libc/stdio/puts.c.html#puts I saw
these lines:

#include /* <stdio.h> */
#include /* <string.h> */

what's that??


2) Why does the standard allow for two definitions of NULL:
#define NULL ((void *) 0)
#define NULL 0

Why not just 0? Isn't 0 the "C language keyword for a null-pointer" ?


3) What does it mean: 16bit/32bit/Nbit OS and/or program? How does
this affect programs written in C?


4) Which is the proper way to printf a size_t variable: %u or %lu or
...


5) I'll think about this one.


TIA

thomas haupt

unread,
Jul 18, 2001, 5:17:00 AM7/18/01
to
Previously, you (rihad) wrote:
> Hi there, I have some unsorted questions:
>
> 1) Here - http://www.ajk.tele.fi/libc/stdio/puts.c.html#puts I saw
> these lines:
>
> #include /* <stdio.h> */
> #include /* <string.h> */
>
> what's that??

Maybe He-Whose-Postings-Should-Always-Be-Read might be able to c..ly comment on this one, since the copyright notice on the piece of code in question says

* This code is derived from software contributed to Berkeley by
* Chris Torek.

Although I'm inclined to doubt that he himself wrote those two lines as they are, since there is at least one C-compiler choking miserably on them.


> 2) Why does the standard allow for two definitions of NULL:
> #define NULL ((void *) 0)
> #define NULL 0
>
> Why not just 0? Isn't 0 the "C language keyword for a null-pointer" ?
>
>
> 3) What does it mean: 16bit/32bit/Nbit OS and/or program? How does
> this affect programs written in C?
>
>
> 4) Which is the proper way to printf a size_t variable: %u or %lu or
> ...
>
>
> 5) I'll think about this one.
>
>
> TIA
>

--
T. Haupt

BitCtrl Systems GmbH
Weissenfelser Str. 67
04229 Leipzig

Phone: +49 (0)341 49067 0
Phax: +49 (0)341 49067 15
eMail: f...@bitctrl.de

Richard Bos

unread,
Jul 18, 2001, 8:35:50 AM7/18/01
to
rihad <ri...@mail.ru> wrote:

> Hi there, I have some unsorted questions:
>
> 1) Here - http://www.ajk.tele.fi/libc/stdio/puts.c.html#puts I saw
> these lines:
>
> #include /* <stdio.h> */
> #include /* <string.h> */
>
> what's that??

As far as ISO C is concerned, it's broken. It may be a compiler-specific
thing.

> 2) Why does the standard allow for two definitions of NULL:
> #define NULL ((void *) 0)
> #define NULL 0
>
> Why not just 0? Isn't 0 the "C language keyword for a null-pointer" ?

It allows for more, actually. A null pointer constant is:
- any constant integer expression that evaluates to 0;
- any of the previous, cast to void *.
So (4-4) is a null pointer constant. So is ((void *) (sizeof(char)/2)).

NULL may be defined as _any_ valid null pointer constant.

> 3) What does it mean: 16bit/32bit/Nbit OS and/or program? How does
> this affect programs written in C?

It has to do with whether the "natural" integer for the computer has 16,
32, N bits. This depends on the processor, the internal bus, sometimes
the mode the processor is in, and probably whether the compiler writer
was lazy or not.
If the C program was written well, it shouldn't really affect it much.
What it does affect is the interlegibility of data written by one
implementation and read by another, for example.

> 4) Which is the proper way to printf a size_t variable: %u or %lu or

Cast to unsigned long, _then_ print with %lu. This may lose information,
but is the best you can do in C89. In C99, don't cast, use %su instead.

Richard

Kalle Olavi Niemitalo

unread,
Jul 18, 2001, 8:42:19 AM7/18/01
to
rihad <ri...@mail.ru> writes:

> 4) Which is the proper way to printf a size_t variable: %u or %lu or
> ...

In C99, it is:

size_t size = sizeof(int);
printf("%zu", size);

With older standards, you can use:

printf("%lu", (unsigned long) size);

That doesn't work right if the compiler actually follows C99 and has a
size_t that is larger than unsigned long, and size happens to be more
than ULONG_MAX too; hopefully that will be rare.

For full portability, you could make a loop that computes the digits
one by one.

Richard Bos

unread,
Jul 18, 2001, 8:45:58 AM7/18/01
to
in...@hoekstra-uitgeverij.nl (Richard Bos) wrote:

> rihad <ri...@mail.ru> wrote:
>
> > 4) Which is the proper way to printf a size_t variable: %u or %lu or
>

> In C99, don't cast, use %su instead.

Duh. Me big lout. Me no read so good. Make that %zu.

Richard

rihad

unread,
Jul 18, 2001, 9:48:34 PM7/18/01
to

please don't joke on such intimate matters :)
did you mean %hu?

Chris Torek

unread,
Jul 18, 2001, 10:29:58 AM7/18/01
to
>Previously, you (rihad) wrote:
>> Hi there, I have some unsorted questions:
>>
>> 1) Here - http://www.ajk.tele.fi/libc/stdio/puts.c.html#puts I saw
>> these lines:
>>
>> #include /* <stdio.h> */
>> #include /* <string.h> */
>>
>> what's that??

In article <Voyager.01071...@qnxserver.bitctrl>


thomas haupt <f...@bitctrl.de> wrote:
>Maybe He-Whose-Postings-Should-Always-Be-Read might be able to
>c..ly comment on this one, since the copyright notice on the piece
>of code in question says
>
> * This code is derived from software contributed to Berkeley by
> * Chris Torek.
>
>Although I'm inclined to doubt that he himself wrote those two lines

>as they are ...

Indeed I did not. The page looks likely to have been generated
by some C-to-HTML program; perhaps that program has a bug.
--
In-Real-Life: Chris Torek, Wind River Systems
El Cerrito, CA, USA Domain: to...@bsdi.com +1 510 234 3167
http://claw.eng.bsdi.com/torek/ (not always up) I report spam to abuse@.

Richard Bos

unread,
Jul 18, 2001, 10:40:47 AM7/18/01
to
rihad <ri...@mail.ru> wrote:

> On Wed, 18 Jul 2001 12:45:58 GMT, in...@hoekstra-uitgeverij.nl (Richard
> Bos) wrote:
>
> >in...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
> >
> >> rihad <ri...@mail.ru> wrote:
> >>
> >> > 4) Which is the proper way to printf a size_t variable: %u or %lu or
> >>
> >> In C99, don't cast, use %su instead.
> >
> >Duh. Me big lout. Me no read so good. Make that %zu.
>

> please don't joke on such intimate matters :)

Eh?

> did you mean %hu?

No. In C99 they've added a special facility for portably printing
size_t's; this is the z length specifier.

Richard

Micah Cowan

unread,
Jul 18, 2001, 2:09:34 PM7/18/01
to
rihad <ri...@mail.ru> writes:

No, he meant %zu. For C99, as he said. In current (C90) compilers,
the best we can do is use %lu and make sure and cast size_t to
unsigned long, which he also said.

Micah

--
"Everytime you declare main() as returning void - somewhere a little
baby cries. So please, do it for the children." -- Daniel Fox

Keith Thompson

unread,
Jul 18, 2001, 10:25:46 PM7/18/01
to
Micah Cowan <mi...@cowanbox.com> writes:
[...]

> No, he meant %zu. For C99, as he said. In current (C90) compilers,
> the best we can do is use %lu and make sure and cast size_t to
> unsigned long, which he also said.

The following approach should be reasonably portable to both C90 and
C99 (assume the necessary #include's):
========================================================================
size_t s;
...
#ifdef ULLONG_MAX
/*
* There's an "unsigned long long" type; use it.
*/
printf("%llu", (unsigned long long)s);
#else
/*
* There's no "unsigned long long" type; it should be safe to use
* "unsigned long".
*/
printf("%lu", (unsigned long)s);
#endif
========================================================================

Or, if you want to make the dependence on the version of the standard
more explicit:
========================================================================
size_t s;
...
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
/*
* This is a C99 compiler, so it supports %zu; no need to cast.
*/
printf("%zu", s);
#else
/*
* This is not a C99 compiler; assume size_t is no bigger than
* unsigned long.
*/
printf("%lu", (unsigned long)s);
#endif
========================================================================

The latter will be easier to clean up on that far off day when we no
longer have to worry about pre-C99 compilers.

There's one possible hole in this approach: some pre-C99 compilers do
support "unsigned long long" as an extension. As far as I know,
though, none of them make size_t bigger than "unsigned long".

--
Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.

0 new messages