473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

64 bit portability, size_t, and printf format strings

http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx
describes the integer format modifiers accepted by Microsoft's printf.

http://www.gnu.org/software/libc/man...nversions.html
describes the integer format modifiers accepted by glibc's printf.

They differ (no big surprise), but the difference that's
bugging me today is how size_t integers are handled.
It looks like one has to do %Id (that's a capital i) in windows, and
%zd everywhere else.
This didn't use to matter much, but now that 64 bit portability is
becoming important, source code that used to happily print size_t's
with %d
is hurting. The way I've been dealing with it is to imitate
<inttypes.h>
and define a macro holding the format char for size_t, say

#ifdef MSVC
#define PRIdS "Id"
#else
#define PRIdS "zd"
#endif
....
printf("sizeof(foo_t) is %" PRIdS ".\n", sizeof(foo_t));

but that's awful ugly. I don't suppose we could convince Microsoft to
push out an updated msvcrt.dll that supported the 'z' format modifier
as an alternative to their 'I' modifier?
- Dan

Jun 29 '06 #1
5 27754


dank wrote On 06/29/06 13:12,:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx
describes the integer format modifiers accepted by Microsoft's printf.

http://www.gnu.org/software/libc/man...nversions.html
describes the integer format modifiers accepted by glibc's printf.
[...]


You could dodge the issue by using a C90 work-around:

printf ("%lu\n", (unsigned long) sizeof something);

.... on the assumption that no legitimate object will be
larger than ULONG_MAX bytes.

--
Er*********@sun.com

Jun 29 '06 #2
On 29 Jun 2006 10:12:34 -0700, "dank" <da************@gmail.com> wrote
in comp.lang.c:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx
describes the integer format modifiers accepted by Microsoft's printf.

http://www.gnu.org/software/libc/man...nversions.html
describes the integer format modifiers accepted by glibc's printf.

They differ (no big surprise), but the difference that's
bugging me today is how size_t integers are handled.
It looks like one has to do %Id (that's a capital i) in windows, and
%zd everywhere else.
This didn't use to matter much, but now that 64 bit portability is
becoming important, source code that used to happily print size_t's
with %d
is hurting. The way I've been dealing with it is to imitate
<inttypes.h>
and define a macro holding the format char for size_t, say

#ifdef MSVC
#define PRIdS "Id"
#else
#define PRIdS "zd"
#endif
...
printf("sizeof(foo_t) is %" PRIdS ".\n", sizeof(foo_t));

but that's awful ugly. I don't suppose we could convince Microsoft to
push out an updated msvcrt.dll that supported the 'z' format modifier
as an alternative to their 'I' modifier?
- Dan


The EU has been trying to "convince" Microsoft to comply with the
terms of an antitrust settlement for two years.

Feel free to try to "convince" them to add something to their
implementations merely because it conforms to a non Microsoft specific
standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 29 '06 #3
dank wrote:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx
describes the integer format modifiers accepted by Microsoft's printf.

http://www.gnu.org/software/libc/man...nversions.html
describes the integer format modifiers accepted by glibc's printf.

They differ (no big surprise), but the difference that's
bugging me today is how size_t integers are handled.
It looks like one has to do %Id (that's a capital i) in windows, and
%zd everywhere else.
This didn't use to matter much, but now that 64 bit portability is
becoming important, source code that used to happily print size_t's
with %d
is hurting.
That's because you shouldn't print size_t's with %d; assuming that a size_t
will convert to an int "happily" is just silly. At the very least print them
as unsigned. The standard workaround for not having a size_t format
specifier (a great many systems don't, and C89 itself didn't) is to convert
the size_t to an unsigned long and print with %lu. Although this can still
be wrong, it's far less likely to be wrong than %d.
The way I've been dealing with it is to imitate <inttypes.h> and define a
macro holding the format char for size_t, say
#ifdef MSVC
#define PRIdS "Id"
#else
#define PRIdS "zd"
#endif
...
Not bad, but I'd make it %Id and %zd (no need to split them there just
because % is a common character) and make %lu the default, not %zd. That
will only work on systems with a C99-ready C library. You could also add a
check for long long support to use %llu. Not all systems support that
either, but it's more common than %zd support.

To make %lu and %llu work, of course, you need some typedefs for the type
used to print size_t's, so you get something like

printf("sizeof(foo_t) is " SIZE_T_FMT_SPEC ".\n", (size_t_print_type)
(sizeof(foo_t)));
printf("sizeof(foo_t) is %" PRIdS ".\n", sizeof(foo_t));

but that's awful ugly.
The least-effort solution that will get things to work everywhere is allowed
to be ugly. You can alternatively champion the cause of C99 support, but
while worthy, it's not as likely to be successful in the short run.
I don't suppose we could convince Microsoft to push out an updated
msvcrt.dll that supported the 'z' format modifier as an alternative to
their 'I' modifier?


Don't hold your breath. C libraries aren't updated that often. Microsoft's
is no exception. Even *if* Microsoft updated the CRT, it'll be years before
everyone has the updated version. (I do mean years, not some arbitrarily
long time.) The situation is somewhat less dire on Unix systems, but don't
kid yourself into thinking that there's universal support there today.

S.
Jun 29 '06 #4
Skarmander wrote:
dank wrote:

<snip>
The way I've been dealing with it is to imitate <inttypes.h> and define a
macro holding the format char for size_t, say
#ifdef MSVC
#define PRIdS "Id"
#else
#define PRIdS "zd"
#endif
...


Not bad, but I'd make it %Id and %zd (no need to split them there just
because % is a common character)


Well, no, but you may want to add flags and field width and whatnot, so
better to keep the % out of there. Although I don't imagine you'd do much
alternate formatting with a size_t, there's no reason to forbid it.

S.
Jun 29 '06 #5
dank wrote:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx
describes the integer format modifiers accepted by Microsoft's printf.

http://www.gnu.org/software/libc/man...nversions.html
describes the integer format modifiers accepted by glibc's printf.

They differ (no big surprise), but the difference that's
bugging me today is how size_t integers are handled.
It looks like one has to do %Id (that's a capital i) in windows, and
%zd everywhere else.
No. It should be %Iu on Windows and %zu everywhere else. Remember that
size_t is an unsigned integer type!
This didn't use to matter much, but now that 64 bit portability is
becoming important, source code that used to happily print size_t's
with %d
is hurting.


Yes. However on all the 64-bit compilers I've used so far, 'unsigned
long' moved to 64 bits, so you could still use %lu and cast the size_t
value to unsigned long.

--
Simon.
Jun 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
16
by: Xenos | last post by:
Is there a standard way to determine the max. value of size_t as a compile-time constant? Will: #define SIZE_T_MAX ((size_t) -1) work in all cases, or just on 2s comp. machines? DrX
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
6
by: David Mathog | last post by:
size_t varies from system to system. This occasionally leads to coding issues such as: (void) sprintf(msg,"length of buffer: %d",strlen(msg)); which worked fine on a 32 linux but triggered...
25
by: Joakim Hove | last post by:
Hello, I have code which makses use of variables of type size_t. The code is originally developed on a 32 bit machine, but now it is run on both a 32 bit and a 64 bit machine. In the code...
93
by: jacob navia | last post by:
In this group there is a bunch of people that call themselves 'regulars' that insist in something called "portability". Portability for them means the least common denominator. Write your code...
6
by: ray.webster | last post by:
Should the following work *if* I have a c99 compiler please? #include <stdio.h> int main(void) { size_t t = 42; # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
10
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.