Re: printf() and void *



David Tiktin said:

On 02 Mar 2008, Richard Heathfield <rjh@xxxxxxxxxxxxxxx> wrote:

<snip>

Well, no pointer value can be printed portably, since the output
associated with %p is implementation-defined.

I'm a little unclear on the point you're making here. I would have
said that the code:

#include <stdio.h>
#include <limits.h>

int main(void)
{
printf("%d\n", INT_MAX);

return 0;
}

is a completely portable way to print the value of INT_MAX even
though what's printed is obviously implementation defined.

Right - the technique is portable, but the value isn't. :-)

<snip>

Can't you write to a
text file portably in C even though the result is implementation
defined?

Again, the technique is portable, but the value isn't. Consider:

#include <stdio.h>

int main(void)
{
FILE *fp = fopen("foo.txt", "w");
if(fp != NULL)
{
if(fputs("A\n", fp) != EOF && fclose(fp))
{
puts("All is well.");
}
}
return 0; /* if fputs failed, the stream will be closed on exit! */
}

If "All is well" is displayed (under any conforming hosted implementation),
we have created a text file, which in C terms contains one line comprising
'A' and a newline character. So the technique is portable.

But what have we actually created, in what we might call absolute terms? On
a PC under Windows, the file will contain three bytes, with values 65, 13,
and 10 (obviously I'm using decimal representation here). On an old-style
Mac, it would contain just two bytes: 65, 13. On a Linux box (or, I
*think*, a modern Mac), it would contain 65, 10. On an IBM mainframe,
well, don't ask(!), but to start off with, that 'A' would be 193 rather
than 65. The point is that, whilst the output can be interpreted
consistently within the machine/OS/implementation combination, moving that
file to another (disparate) system will (or at least may) result in that
interpretation becoming invalidated unless some kind of data massage is
performed.

So I guess it all depends on what we mean by "portable"!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: All programs are undefined, Re: Why this works???
    ... terms of the capabilities of a particular machine about whose details ISO ... void printat(const char *s, int x, int y, int bg, int fg) ... If this technique were not well-defined under the above circumstances, ... these programs were able to rely on the well-definedness of this technique ...
    (comp.lang.c)
  • Re: Prime numbers
    ... The technique I use has fixed-time modular reductions, ... Slows down when FPInt conversions are slow, ... You get different behaviour depending on how int() works (which ... The man who is always worrying about whether or not his soul would be ...
    (sci.math)
  • Re: Press n Seal and Crayola Markers
    ... I'm still unclear how this works and what a Press and Seal is, ... and this sounds like an exciting technique I'd like to try. ... flu and my head is all cottony.) ...
    (rec.crafts.textiles.quilting)
  • Re: Problems linking a BCB-DLL (.lib) under MSVC
    ... that the opposite way was quiet easy by simply using ... one single IMPLIB command! ... // tried another return value (int) here: ... > stub-generating technique should always work, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: When to introduce exception safety
    ... >| exception. ... because we already have a technique that can handle cases where ... > Array(int N) throw ...
    (alt.comp.lang.learn.c-cpp)