Path: csiph.com!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: iso646.h Date: Mon, 29 Jan 2024 12:48:03 -0800 Organization: None to speak of Lines: 60 Message-ID: <871q9z7toc.fsf@nosuchdomain.example.com> References: <87frym7l3p.fsf@nosuchdomain.example.com> <87cytl6p20.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: dont-email.me; posting-host="7785f266d7a579ac4bdae19d9302dff6"; logging-data="659781"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DYSHI/TqyJ5GAwZ6asfka" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:MAr8jwZTewSgW4S+bD2oXypGfLA= sha1:T7djmSPk9aPEeqIKlnpOrUNBN3U= Xref: csiph.com comp.lang.c:381194 Janis Papanagnou writes: > On 29.01.2024 00:00, Keith Thompson wrote: >> Janis Papanagnou writes: >>> On 27.01.2024 21:17, Malcolm McLean wrote: >> [...] >>>> printf() is the >>>> main C interface to that, and supports integers, floats and strings, to >>>> a first approximation. >>> >>> It's not an approximation; printf() is _restricted_ to these types (and >>> a few more variants of these few basic types, to be correct). >> >> printf also supports pointer values with "%p". And it support single >> characters, which are not strings. >> >> strings of cousre absolutely do not have to be ASCII. Using printf to >> print data with embedded null bytes is tricky > > I haven't tried with a pure C compiler but with a C++ compiler > it works fine (see below). - Just don't make the mistake to try > to embed a NUL inside a string constant, like > > printf ("%s\n", "My\0string"); > > which won't work as some may expect (you will only see "My"). Right, that's what's tricky about it. printf with "%s" treats the corresponding argument as a pointer to a string, which means that it ignores anything after the first null character. If you want to print "My", followed by a null byte, followed by "string", you'll have to use a different technique. And the argument doesn't have to be a string constant for this to be relevant. Printing arbitrary data with printf("%s", ...) is likely to fail if the data contains null bytes. fwrite() is likely to be a better choice. Both printf() and fwrite() work as if by repeated calls to fputc(). [...] >> it can be perfectly sensible (though not 100% portable) to send >> binary data to stdout. > > Not observed (by me) in the environments I was using the past decades. To list the contents of a .tgz file, you can do: zcat foo.tgz | tar tf - zcat and tar are implemented in C (I think; if not, they certainly could be). In this command, zcat is writing binary data to stdout, and tar is reading binary data from stdin. (GNU tar can also read directly from a gzipped tar file, but that doesn't wouldn't illustrate the point.) Of course all this can fail on systems with a strong distinction between text and binary streams. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Medtronic void Void(void) { Void(); } /* The recursive call of the void */