Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: I found a stack thread <strong><em><a href="https://stackoverflow.com/questions/5929711/c-function-with-no-parameters-behavior">C function with no parameters behavior</a></em></strong> which gives a very succinct and specific, accurate answer. PMG's comment at the end of the answer taks about UB. Below were my original thoughts, which I think are along the same lines and explain why the behaviour is UB..</p> <blockquote> <blockquote> <p>Questions: is the result of a function call with missing arguments defined? </p> </blockquote> </blockquote> <p>I would say no... The reason being is that I think the function will operate as-if it had the second parameter, but as explained below, that second parameter could just be junk.</p> <blockquote> <blockquote> <p>If so, what value will the function receive for the unspecified argument?</p> </blockquote> </blockquote> <p>I think the values received are undefined. This is why you could have UB.</p> <p>There are two general ways of parameter passing that I'm aware of... (Wikipedia has a good page on <a href="http://en.wikipedia.org/wiki/Calling_convention" rel="nofollow noreferrer"><strong><em>calling conventions</em></strong></a>)</p> <ol> <li>Pass by register. I.e., the ABI (Application Binary Interface) for the plat form will say that registers x &amp; y for example are for passing in parameters, and any more above that get passed via stack...</li> <li>Everything gets passed via stack...</li> </ol> <p>Thus when you give one module a definition of the function with "...unspecified (but not variable) number of parameters..." (the extern def), it will not place as many parameters as you give it (in this case 1) in either the registers or stack location that the real function will look in to get the parameter values. Therefore the second area for the second parameter, which is missed out, essentially contains random junk.</p> <p>EDIT: Based on the other stack thread I found, I would ammended the above to say that the extern declared a function with no parameters to a declared a function with "unspecified (but not variable) number of parameters".</p> <p>When the program jumps to the function, that function assumes the parameter passing mechanism has been correctly obeyed, so either looks in registers or the stack and uses whatever values it finds... asumming them to be correct.</p> <blockquote> <blockquote> <p>Otherwise, would the Sun C compiler of ca. 1996 (for Solaris, not VMS) have exhibited a >> predictable implementation-specific behavior</p> </blockquote> </blockquote> <p>You'd have to check your compiler documentation. I doubt it... the extern definition would be trusted completely so I doubt the registers or stack, depending on parameter passing mechanism, would get correctly initialised...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload