However --- and I speak from personal experience at high school and
college levels --- it is almost always easier to prove to the instructor
that the code is faulty, than it is to try to guess what the instructor
was thinking.
<OT>
I'm tempted to change the subject now to "[OT]: Challenging instructors" or
something. This could be an interesting topic, but it seems pretty OT.
In this case, the OP had two choices: write down what he thought the
instructor wanted, or raise his hand and say "Excuse me, there seems to
be a bug in this program... could you clarify the problem, please?"
If the OP knew there was a bug. But the question he posted was about what
the program would do. How many students, in that position, and with an
instructor who writes exam questions like /that/ (the declaration of main
was, IMO, particularly egregious), would be likely to detect a "flaw" in
the question? To be able to ask, you have to have a handle on what to ask.
From what I see, the most likely question to come up in this example would
have been regarding the signed-ness of char. I would /hope/ the fact that
this is implementation-dependent would have been discussed in class, but I
wouldn't bet on it. Given that, I'd put the chances near zero that there
was any discussion in that course about how not including <stdio.h> invokes
UB with printf (I can't even say I fully understand that myself right now),
or what happens when the last line of output is unterminated. Unless they'd
had access to a C99 compiler in strict mode, I doubt these issues would
have shown up even if they'd been written into programs.
I get the feeling that if someone /did/ bring up the stdio.h or line
termination issue, the instructor either wouldn't know what the student was
talking about, or brush it off with a roll of the eyes and a "Just
assume...."
Best case, the instructor realizes the problem is unsolvable and gives
everybody in the class extra credit. Worst case, the instructor says,
"char is unsigned and <stdio.h> is included," and the OP is back where
he started.
The OP had better have hoped for the best-case scenario, since his
guess turned out to be wrong. By drawing attention to the bugs in
the program, he would have maximized his chances of getting credit for
the question.
A good instructor loves to be corrected and/or challenged by students, but
I've (unfortunately) also seen some that it would have been a grievous
tactical error to point out some of those issues to. Unfortunately, those
latter types often tend to have a disproportional amount of political clout
in the learning institution, for some Godforsaken reason...
Again, I speak from experience, and am not being pedantic merely
for pedantry's sake. If the OP can learn to recognize common errors
in C programs, then he stands to benefit in the short term. (In the
long term, he won't be dealing with contrived C programs anymore, and
he'll have to learn common *correct* ways to *write* C programs. But
this is a good way to start.
Reading this newsgroup could definitely accelerate one's coming-of-age as a
C programmer (though perhaps not as much as posting and then being brave
enough to actually read the responses). At the beginning level, however, I
suspect most students' neurons are busy enough trying to remember how to
define and use pointers, stay within array bounds, understand flow control,
and (with the especially clueless instructors) memorizing the precedence
table, to have the capacity left to pursue distinctions of UB between
signed and unsigned integral values. The ones who do, are probably destined
to be future regulars in this group ;-)
-leor