I'm not too familiar with programming in C (I've only done a few small projects in the language), however, my professor said something about it's behavior today that left me a bit confused.
What he said was that this code will sometimes not print anything at all (I copied down exactly what was on the board, I believe this is pseudocode for C since "print" is not in C):
int a = ___________;
int b = ___________;
if (a < b)
print (“<“);
if (a > b)
print (“>”);
if (a==b)
print(“=”);
Basically, there is something you could store in those int variables where none of these conditions would be met (the ____ isn't actual code obviously, it just represents that something is there). It doesn't necessarily need to be some int number that fills those blanks...it could be anything in the world (and there can be stuff that happened before this code).
What is it that could fill those blanks and wouldn't produce any result, and why?
p.s - it had something to do with an overflow, undefined behavior, out of bounds error, or something of the like
p.p.s - I have serious trouble believing this professor was mistaken. He is more knowledgable about programming than anyone I've ever come into contact with. I'm convinced there is some case where this is true.
All that is necessary is for one of the int initialisations to generate a run-time exception. The program will then terminate prior to any of the comparison tests. E.g.
int b = *(int *)0;
I guess it's the _ part that matters. If the underlined part contains code results UNDEFINED behavior, and the following code with comparisons get optimized by a "clever" compiler wrt the undefined behavior, the ultimate behavior of this piece of code is undefined. And not printing anything is a reasonable undefined behavior.
p.s. According to Wikipedia, division by zero results in undefined behavior, although most compilers define it an error. And IIRC signed integer overflow also causes undefined behavior, although again this usually cause runtime exception or even compilation error. So if a and b are declared as
int a = 1 / 0;
int b = INT_MAX + 1;
the situation your prof describes may occur. But remember, the behavior is undefined, so whatever the compiler chooses the program to behave might be considered conformant with the standard.
If you have an architecture with int numbers in one's complement, there exist two zeros: One with all bit set to 0 and one with all bit set to 1, aka positive and negative zero. Both zeros should compare equal, but a buggy compiler may not see that so ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With