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 wher开发者_如何转开发e 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 ;-)
For int this is not true, because they have no "special" values.
p.s - it might have had something to do with an overflow?
I think this is what your professor meant:
#include <stdio.h>
#include <stdint.h>
int main(int argc, char** argv)
{
uint8_t a = 100;
uint8_t b = 200;
a = a + b; /* a = 300 in your head. This overflows, so 300-256=44 */
if ( a > b )
{
printf("%u > %u", a, b);
}
else if ( a < b )
{
printf("%u < %u", a, b);
}
else
{
printf("%u == %u", a, b);
}
return 0;
}
Specifically, I am using a fixed width 8-bit unsigned integer, which can hold at maximum 8 bits (surprise) which represents 256 as the largest possible integer. You can overflow this field by adding to it an amount that can't be represented in this size, in which case what you're left with is a wrap-around effect.
There is no actual ambiguity in the if comparison logic, since that's actually part of the processor (if translates to one of the comparison / jump instruction combinations). The issue is that you might have programmed it reasonably expecting it to work, not realising that number representations on a computer all have fixed representations. I can't see anything I could put into those registers that would possibly escape all of "less, greater or equal".
Note I've used the C99-introduced fixed size integers for clarity and small number sizes so we can do it in our head.
Maybe the defintion of print
is:
void
print(const char*) {
}
Given that "there can be stuff that happened before this code"...
#include <stdio.h>
#include <math.h>
int main(void)
{
#define int float
int a = nanf(NULL);
int b = nanf(NULL);
if (a < b)
printf("<\n");
if (a > b)
printf(">\n");
if (a == b)
printf("==\n");
return 0;
}
a = 0, b = 5 / a : divide by zero
精彩评论