[LCP]what value does the foo() return?

Greg Black gjb at gbch.net
Tue Jan 6 10:28:02 UTC 2004


On 2004-01-05, David Spencer wrote:

> The interviewer could have been probing for how much you knew about what 
> compilers actually do.  Undefined is the correct answer; however many 
> compilers I have met return int values from functions in a CPU register, 
> so for those compilers the return value would be whatever is in that 
> particular register when the function returns.

This is so self-evident that it isn't worth saying.  Whether the
particular architecture puts the return values in a register or
on the stack or in some magic global is utterly beside the
point.  What matters is that, in the example given, using the
value of a function call where a return statement without an
expression is executed results in undefined behaviour.

Undefined behaviour is just that -- the compiler is at liberty
to generate whatever code it likes, ranging from no code to an
abort(), to setting fire to your house.

> The problem with the answer "Undefined" is 
> that it's a one word answer, and interviewers usually want to get to 
> know a bit more about you.

So if the question is: "Are you male?", you think the answer
should be more than "Yes" or "No"; when a one word answer is the
complete answer, then it's perfectly adequate.

> Any idiot can read a book and determine the 
> answer to the question, so this answer doesn't in itself differentiate 
> between an experienced programmer and that idiot.

No idiot can read the C Standard and successfully answer
questions based on that.  Suggesting that people who can quote
the Standard in defence of their answers are idiots is an
indicator that you probably don't really understand what this
business of undefined behaviour is all about.

> As a quick test I tried the following program:

This is the surest evidence that you don't understand.  As soon
as somebody tries to make a point about a defined language like
C by illustrating what happens with some example code, then it's
clear that they don't get it.  The behaviour of correct programs
is defined by the language and needs no sample code.  Incorrect
programs, e.g., those that exhibit undefined behaviour, are just
incorrect programs and their behaviour is not interesting.

Greg



More information about the linuxCprogramming mailing list