Sujet : Re: Expression statements (was Re: Meaning of "expression")
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 11. Jun 2026, 23:37:03
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <110fdaf$1naub$11@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
User-Agent : Mozilla Thunderbird
On 2026-06-11 21:13, James Kuyper wrote:
On 2026-06-11 14:12, Janis Papanagnou wrote:
On 2026-06-10 00:34, Keith Thompson wrote:
...
For I/O, the equivalent of printf is a procedure. In C,
printf("Hello, world\n") returns a negative result to denote an
error (and that value is often ignored).
>
Erm, I hope that above printf() call does not create an error, but
returns the number of characters in the printed text. ;-)
Hope is nice. I hope, in particular, that you're aware that there are
not guarantees on that matter?
Oh, actually I indeed thought that printing a constant string would not
create any error that would then be indicated by printf's return value.
I'd indeed also expected that, say, printing a string value with a '%d'
specifier would produce an error, but I saw that it doesn't; while the
compiler creates just a warning, execution provides some random output
and a _non-negative_ string-length value as printf's return value. Not
exactly what I'd expect from a language.
Concerning the "guarantees" that you're asking for I sadly have to say
that I meanwhile expect nothing sensible at all any more from "C". ;-)
But to be more serious again...
The man-page is very unspecific on that; 'man 3 printf' says:
"If an output error is encountered, a negative value is returned."
Now of course an error can occur with that simple 'printf' above, for
example, by issuing an 'fclose (stdout);' before the 'printf (...);'
But what can I as a C-programmer derive from that; how would one act
on that. (That's just rhetorical.)
Obviously (because of that?) I've never seen anyone test such a call
by, say,
int rc = printf("Hello, world\n");
if (rc < 0) {
/* umm.. */
}
Are you - plural, all CLC audience - writing such code with 'printf()',
honestly? - Same question with 'int rc = fclose (...);' - what can one
do about that, then? (Write a logfile entry, maybe? - and then?)
But yes, I'm aware of negative OS function or library function output.
Our rules (back in my C/C++ days) suggested to catch any sensible and
possible error indications to quickly localize any potential issues.
Janis
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal