Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

http://stackoverflow.com/questions/8698048/behaviour-of-non-const-int-pointer-on-a-const-int

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 ?

Thanks.

share|improve this question
2  
Just write "this is undefined" next to it. The document containing all UBs is the C standard. –  rightfold Jan 2 '12 at 7:40
    
Link to the doc ?@WTP –  SHOUBHIK BOSE Jan 2 '12 at 7:42
    
I guess it is iso.org/iso/iso_catalogue/catalogue_tc/… –  rightfold Jan 2 '12 at 7:49
    
oh, well. is an option regarding nasal demons available? –  ZJR Jan 2 '12 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 '12 at 15:29
add comment

2 Answers

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.

share|improve this answer
2  
+1 sometimes the professors think they know better when they don't. –  ZJR Jan 2 '12 at 8:32
add comment

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.

EDIT

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.)

share|improve this answer
    
Cool, very often one encounters the printf("%d %d",a,a++); similar to what you were asked. –  SHOUBHIK BOSE Jan 2 '12 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 '12 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 '12 at 12:57
    
@Mike: Fair enough, I just did. The C99 standard (or the draft of it that I got a hold of at least) defines undefined behavior to be when the standard "imposes no requirement" on the behavior (section 3.4.3), i.e. it might do anything at all. It defines unspecified behavior to be when the behavior will be one out of multiple defined possibilities. I.e. it is not defined whether value a or value b is used, but it is defined that one of them will be used. It actually cites the order in which function arguments are evaluated as an example of this (section 3.4.4). –  sepp2k Jan 2 '12 at 14:00
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 '12 at 14:50
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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