Ryan Reich said:
True enough, but as a matter of aesthetics I find that particular type of
call to be just a little lacking.
What does it lack? I've never encountered anyone with
a reasonable amount of C experience that had trouble
grasping it at a glance.
I suppose I really just have a problem
with the idea of 'errno',
as it seems to me that any function which can
experience an internal error ought to indicate such as its return value,
And what kind of 'internal error' would e.g. 'strcat()' encounter?
(I would not classify receiving bad input, or insufficient storage
for output as an 'internal' error, but a coding error.)
rather than employing a global variable for no good reason.
The 'str...()' functions don't use errno.
It does mean
that you can't chain calls together, but I think that just makes the code
more readable anyway. Something like
if(strcat(a,b) != 0)
indicate_error();
And what would you consider to comprise an 'internal error' in a 'str...()'
function?
printf("%s", a);
has the stylistic advantage of indicating clearly that you intend to
concatenate 'a' and 'b'
IMO the very appearance of 'strcat()' as an expression or subexpression
indicates it very clearly.
(rather than, for example, return a string which is
their concatenation but leave them unchanged.
Returning a separate string poses problems, since an array cannot be
returned from a function, meaning that memory management becomes
even more of an issue that it already is.
We all know what 'strcat()'
does, of course, but it still looks that way
Looks what way? That it 'returns a string'? Well, yes it does,
a pointer to the destination. To what should this pointer point?
and using the return value
turns the actual task of the function into a side-effect,
It already is. Returning a value or not doesn't change this.
which is not a
good idea to me), of checking whether it was done cleanly
How could it not be done 'cleanly'? (imo misuse, e.g. array overflow
doesn't qualify as a failure of 'strcat()', but of the coder).
(maybe 'strcat()'
isn't the best function to illustrate this with, since it can't really fail
without crashing the program entirely due to buffer overflow), and of
handling the error before you do anything with the result. Plus it has
each line do one well-defined action rather than, in the example you gave,
four ill-defined ones
All the actions are well-defined.
(concatenate, NOT check error conditions,
What error conditions? It's simply impossible to check for every
possible programmer mistake. E.g. how could 'strcat()' check for
overflow when it cannot know the size of the input or output buffers?
print, and
possibly fail anyway because you may have been distracted by all the
typing
All the typing? Huh? It's actually more typing to break it
into more lines. But I wouldn't call doing so 'inferior' to
doing it all in one expression, but simply a stylistic issue.
and forgot to include the format string).
'printf()' requires at least one argument. If one is not
supplied, the compiler is required to diagnose it.
-Mike