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 5 27810
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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
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_t ype)
(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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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---
#include "stdio.h"
#include "string.h"
void printText(char c){
|
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
|
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 %% should be used.
Wouldn't it have been better (from design perspective) if the same
escape character had been used in this case too.
Forgive me for posting without verfying things with any standard
compiler, i don't have the means for now.
|
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 errors with gcc
on an X86_64 system. (Ok, it should probably have been a %u rather
than a %d, but since msg was always going to be short it didn't
affect the program's execution.)
|
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 have statements like this:
size_t buffer_size;
printf("Total buffer size: %ud bytes \n",buffer_size);
| |
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 so that it will compile in all old and broken
compilers, preferably in such a fashion that it can be moved with no
effort from the embedded system in the coffe machine to the 64 bit
processor in your desktop.
|
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
|
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 application that the code be
speed optimized ) I use reinterpret_cast to alias a block of chars read
in from disc as a block of integer "words". My existing code (see
simplified code below) appears to work well enough on the platforms
available to me,...
|
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 right thing to use for every var that holds the
number of or size in bytes of things.
* size_t should only be used when dealing with library functions.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |