Recently I came across a number of undefined features in C, one of them being the following:


Could someone tell me what should I answer when such a question is asked during online exams ( and None of These / Undefined option is not available ) ?

Also, could you point me to a document specifying the most commonly encountered undefined behaviours in C ?


  • 2
    Just write "this is undefined" next to it. The document containing all UBs is the C standard.
    – user4595
    Jan 2, 2012 at 7:40
  • Link to the doc ?@WTP Jan 2, 2012 at 7:42
  • I guess it is iso.org/iso/iso_catalogue/catalogue_tc/…
    – user4595
    Jan 2, 2012 at 7:49
  • oh, well. is an option regarding nasal demons available?
    – ZJR
    Jan 2, 2012 at 9:33
  • Could you list the options you saw in that exam? We may organize a massive write-in campaign to the exam publisher.
    – rwong
    Jan 2, 2012 at 15:29

2 Answers 2


You can do either of the following:

  1. Check the "right" answer (the one that your experience with the professor tells you that he wants to see). Sometimes the professors think they know better when they don't. You can argue (and get a hit with the grade) or just let them hear what they want to hear.

  2. Approach the professor and raise the issue. Ask a question during the office hours or even in the class. That's what I would do. You don't want to know my GPA.

  3. Write (if possible) an additional answer "This is undefined behavior per standard, but I believe most compilers would do ..." and proceed with #1.

Generally if professors put you in this position it means a lot about their own knowledge and qualifications. But, I don't think you want to put yourself into a position of that annoying student that puts the professor to shame. I did it twice, in one class I ended up with A+, in another with barely a B (after scoring 100% in the final exam). You never know how they'll take it. Some are ready to admit a mistake, some will be very vicious.

  • 2
    +1 sometimes the professors think they know better when they don't.
    – ZJR
    Jan 2, 2012 at 8:32

I presume you are referring to an online exam in which there is no professor to speak to, and no margin to handwrite an objection.

So, what I would do in your shoes is that I would pick the answer that most closely matches "undefined" (something like "prohibited" or "crash") and if none of these are available then I would just pick a guess. Then, I would wait until my exam gets graded, and if my answer to this question is flagged as wrong, I would take it to the dean.

Start looking for the dean's email from now.


The most famous instance of undefined behavior in C is this:

int somefunction( int x, int y );
int a = 5;
int result = somefunction( a, a++ );

The question is "what is wrong with the above call?"

I came across this one in an interview once, but it was just for bonus points, they did not really expect the candidate to know. The answer is undefined behavior because C does not guarantee to you that 'a++' will be evaluated after 'a'. So, somefunction may be called with x=5, y=6 or it may be called with x=5, y=5 or x=6, y=6. (or a whale and a bowl of petunias might fall from the sky <-- EDIT: no, this is not a possibility.)

  • Cool, very often one encounters the printf("%d %d",a,a++); similar to what you were asked. Jan 2, 2012 at 9:41
  • Is that really undefined behavior in the sense of demons flying out of your nose? According to my (somewhat limited) it's simply unspecified whether the above code will call someFunction(5,5) or someFunction(6,5) because the order in which arguments are evaluated is unspecified. But it's still guaranteed that it won't do anything else (as opposed to somefunction(a++, a++), which might do anything it damn well pleases (on account of modifying the same variable twice without a sequence point in between invoking undefined behavior)).
    – sepp2k
    Jan 2, 2012 at 11:35
  • Good point. It depends on what we mean by "undefined behavior". I think "undefined behavior" is when we have an operation whose outcome cannot be deterministically guaranteed by the standard, and the standard chooses to expressly warn against it. But I suppose that this question can be answered by reading the standard, so there is no point in theorizing about it.
    – Mike Nakis
    Jan 2, 2012 at 12:57
  • 1
    @MikeNakis The exact wording is "Furthermore, the prior value shall be read only to determine the value to be stored", which I take to mean that x may only be used "inside" the expression that modifies x. I.e. f(x += x) is fine because here the modification depends on x and thus its unambiguous that x must refer to the old value here. But f(x += y, x) is not fine because here the modification does not depend on the value that is used as the second argument.
    – sepp2k
    Jan 2, 2012 at 14:50
  • 1
    Why did you strike out the whales and petunias? Given the code if (conditionThatWillNeverActuallyArise) DropWhalesAndPetunias(); someFunction(a,a++); a compiler could infer that because the program execution cannot possibly be defined if the above code is reached when conditionThatWillNeverActuallyArise is false, and might be defined if it's true (e.g. if DropWhalesAndPetunias calls exit), the call to DropWhalesAndPetunias() may as well be made unconditional.
    – supercat
    Aug 1, 2015 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.