Chip Salzenberg on 26 Apr 2004 22:44:02 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] Re: #define for 32 vs 64 bit C program?


According to Walt Mankowski:
> Looking through the STL documentation as well as the header files, it
> appears that size() returns a size_type, and size_type is really a
> size_t.  Is size_t guaranteed to be a long?

No, size_t is guaranteed to be unsigned of some size (so you should be
using 'u', not 'd').  If you're doing C++ you should really use:

  cout << "There are " << v.size() << " elements" << endl;

But failing that, glibc at least has the 'z' modifier, which I believe
is POSIX or SUS or one of those standards:

  printf("There are %zu elements\n", v.size());

If you can't count on the 'z' then you need to cast the value to a
known type, and long is a good choice:

  printf("There are %lu elements\n", (unsigned long)v.size());

Finally, C99 has some truly amazingly ugly macros that you're supposed
to use with string pasting:

  printf("There are " SOMETHING_UGLY_HERE " elements\n", v.size());

But as you can tell I don't really like those.
-- 
Chip Salzenberg               - a.k.a. -               <chip@pobox.com>
"I wanted to play hopscotch with the impenetrable mystery of existence,
    but he stepped in a wormhole and had to go in early."  // MST3K
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug