Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Q: stdint format specifiers |
| Date | 2016-08-04 12:44 -0700 |
| Organization | None to speak of |
| Message-ID | <ln8twc1fpw.fsf@kst-u.example.com> (permalink) |
| References | <nnvpf7$1ojc$1@gioia.aioe.org> <282c43f4-9e51-4c3c-9552-6703c60df230@googlegroups.com> |
supercat@casperkitty.com writes:
> On Thursday, August 4, 2016 at 11:11:29 AM UTC-5, Johann Klammer wrote:
>> Does anyone have a coccinelle script to fixup *printf format specifiers to
>> those pesky PRI*** ones? I could not find one on coccinellery <http://coccinellery.org/>.
>
> Such a thing will likely be difficult unless one can keep track of types
> in more detail than most compilers do. If a particular compiler defines
> "int32_t" as an alias for "long", then "%ld" would likely get used on that
> compiler as the formatting code for both "int32_t" and "long". Unless
> a conversion tool can keep track of which 32-bit variables are actually
> of type "uint32_t", it would have no way of knowing which ones should be
> printed out using a "%ld" specifier and which ones require the PRI_* macros.
To be clear, assuming int32_t is a typedef for long, there is no
distinction between a variable of type int32_t and a variable of type
long. int32_t is not a distinct type with the same characteristics as
long; it's a different name for exactly the same type.
Certainly a tool *could* keep track of which typedef is used to define
an object, and choose a printf format based on that. (It would be
tracking additional information that's not defined by the C language;
nothing wrong with that.)
Without that ability, it could use "%ld" for any argument of type
long, whether it was defined using the name "long" or the name
"int32_t". But then the resulting code (assuming you're trying
to generate C source code) wouldn't be portable; it could fail
when compiled for a system where int32_t is an alias for int, and
long is 64 bits. (Unless you regenerate the source code for each
target system; is that an option?)
It's an interesting problem, and not one that the C language gives you a
lot of help with.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Q: stdint format specifiers Johann Klammer <klammerj@NOSPAM.a1.net> - 2016-08-04 18:11 +0200
Re: Q: stdint format specifiers supercat@casperkitty.com - 2016-08-04 09:53 -0700
Re: Q: stdint format specifiers Keith Thompson <kst-u@mib.org> - 2016-08-04 12:44 -0700
Re: Q: stdint format specifiers Johann Klammer <klammerj@NOSPAM.a1.net> - 2016-08-04 22:57 +0200
Re: Q: stdint format specifiers supercat@casperkitty.com - 2016-08-04 14:11 -0700
Re: Q: stdint format specifiers Keith Thompson <kst-u@mib.org> - 2016-08-04 14:19 -0700
Re: Q: stdint format specifiers supercat@casperkitty.com - 2016-08-04 15:08 -0700
Re: Q: stdint format specifiers Keith Thompson <kst-u@mib.org> - 2016-08-04 15:26 -0700
Re: Q: stdint format specifiers Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-08-04 23:19 -0700
Re: Q: stdint format specifiers supercat@casperkitty.com - 2016-08-05 08:04 -0700
Re: Q: stdint format specifiers Keith Thompson <kst-u@mib.org> - 2016-08-05 08:26 -0700
csiph-web