On 4/4/22 13:32, Andrey Tarasevich wrote:
> On 4/4/2022 8:20 AM, James Kuyper wrote:
>> On 4/4/22 00:24, Andrey Tarasevich wrote:
>>> On 4/3/2022 9:23 PM, Andrey Tarasevich wrote:
...
>>>> Secondly, if you simply replaced `sprintf` with `sprintf_s` your code
>>>> became invalid. Your compiler clearly told you where the problem is by
>>>> issuing a diagnostic message. Why did you decide to ignore that
>>>> diagnostic message and ask an abstract question here instead? (And here
>>>> you don't even mention the diagnostic message.)
>>>>
>>>
>>> OK, I missed this part
>>>
>>>> The code I have tried is:
>>>> sprintf_s(outstring, "Year %3d: %10.2f\n", i, arr[i]);
>>>
>>> But my "secondly" still stands.
>>>
>>
>>
>> He said "when I try to make use of sprintf_s() (for latest versions of
>> compilers) it prints nothing". This means that he successfully compiled
>> and linked the code, and suggests that he might not have seen any
>> diagnostics.
>
> Well, that's the problem I'm trying to point out. I'm confident that a
> diagnostic message was emitted by the compiler, but he might not have
> seen it simply because he wasn't paying attention.
That's one possibility, but I was pointing out that another possibility
is that no diagnostic message was emitted, due to his choice of compiler
and compilation options.
> Many C compilers are traditionally/historically configured to report
> certain kinds of hard "errors" as "warnings" by default. And this is
> often the reason inexperienced users often take careless and
> dismissive attitude to such diagnostic messages.
>
> Supplying an argument of incorrect type to a prototyped function is a
> hard error in C (
http://port70.net/~nsz/c/c11/n1570.html#6.5.2.2p2).
I think "mandatory diagnostic" is a better term than "hard error".
"Hard error" might be misinterpreted as implying that a conforming
implementation of C is required to reject such a program.
I mentioned the possibility that he was compiling in C90 mode, precisely
because C90 didn't have prototypes, and a function could be called
without a declaration in scope - no diagnostic was required.
A compiler option that turns off that mandatory diagnostic would render
it non-conforming to C99 and later, but there's nothing unusual about C
compilers having options that render them non-conforming.
> If the diagnostic message is one of those required by the standard
> (e.g. a constraint violation), then the code *isn't* officially
> considered "successfully compiled and linked".
I consider a program to have been "successfully compiled and linked" if
compilation results in a linkable object file, and if linkage results in
an executable program. The fact that a diagnostic is mandatory does not
prohibit either of those outcomes.
> ... Whatever executable an implementation produces in such cases is
> considered a "garbage temporary output file" the implementation simply
> "forgot" to clean up after translation.
No, in many cases implementations will take code for which a diagnostic
is mandatory, translate it in a way the implementor considers useful,
and generate an executable that will produce that useful behavior when
executed. In some cases, this useful behavior is a documented extension.
Allowing code that calls a undeclared function to be compiled the same
way it would have been in C90 would be an example of such "useful
behavior". Such a program could be linkable with module containing an
external definition for that function, if the types of the function's
parameters declared in the function definition are compatible with the
promoted types of the corresponding function call arguments, and the
return type is `int`.
If I see such a mandatory diagnostic, I wouldn't bother trying to use
the resulting executable, but I know that it's entirely possible that
the resulting executable might actually be executable, and might have
reasonable behavior (not necessarily the behavior I wanted), even though
the standard does not require that this be the case.