Liste des Groupes | Revenir à cl c |
bart <bc@freeuk.com> writes:
That was my point about the 3 decades it took to do something about it. In the end nothing really changed.Meanwhile for over 4 decades I've been able to just write 'print foo'By using a different language, which perhaps you should consider
with no format mismatch, because such a silly concept doesn't exist.
THAT's how you deal with it.
discussing in a different newsgroup. We discuss C here.
If foo is an int, for example, printf lets you decide how to printThe format string specified two things. One is to do with the type of an expression, which the compiler knows. After all that's how sometimes it can tell you you've got it wrong.
it (leading zeros or spaces, decimal vs. hex vs. octal (or binary
in C23), upper vs. lower case for hex). Perhaps "print foo" in
your language has similar features.
Yes, the fact that incorrect printf format strings cause undefinedA few years ago I played with a "%?" format code in my 'bcc' compiler and demonstrated it here. The ? gets replaced by some suitable format code. This is done within the compiler, not the printf library.
behavior, and that that's sometimes difficult to diagnose, is a
language problem. I don't recall anyone saying it isn't. But it's
really not that hard to deal with it as a programmer.
If you have ideas (other than abandoning C) for a flexible
type-safe printing function, by all means share them. What are your
suggestions?
foo` is unlikely to be considered practical; I'd want a much more
general mechanism that's not limited to stdio files. Reasonable new
language features that enable type-safe printf-like functions could
be interesting. I'm not aware of any such proposals for C.
And you know why, right? uint64_t is a typedef (an alias) for some>We just can't have size_t variables swilling around in prgrams for thesePOSIX defines a set of strings that can be used by a programmer to
reasons.
specify the format string for size_t on any given implementation.
And here it just gets even uglier. You also get situations like this:
>
uint64_t i=0;
printf("%lld\n", i);
>
This compiles OK with gcc -Wall, on Windows64. But compile under
Linux64 and it complains the format should be %ld. Change it to %ld,
and it complains under Windows.
>
It can't tell you that you should be using one of those ludicrous macros.
existing type, typically either unsigned long or unsigned long long.
If uint64_t is a typedef for unsigned long long, then i is of type
unsigned long long, and the format string is correct.
Sure, that's a language problem. It's unfortunate that code can be
either valid or a constraint violation depending on how the current
implementation defines uint64_t. I just don't spend much time
complaining about it.
I wouldn't mind seeing a new kind of typedef that creates a new type
rather than an alias. Then uint64_t could be a distinct type.
That could cause some problems for _Generic, for example.
C99 added <stdint.h>, defining fixed-width and other integer types using
existing language features. Sure, there are some disadvantages in the
way it was done. The alternative, creating new language features, would
likely have resulted in the proposal not being accepted until some time
after C99, if ever.
I've also just noticed that 'i' is unsigned but the format calls forThe standard allows using an argument of an integer type with a format
signed. That may or may not be deliberate, but the compiler didn't say
anything.
of the corresponding type of the other signedness, as long as the value
is in the range of both. (I vaguely recall the standard's wording being
a bit vague on this point.)
Les messages affichés proviennent d'usenet.