Ambiguity in comparing quantity (in C)?

I am not very familiar with C programming (I only did a few small projects in this language), but my professor said something about this behavior today, which confused me a bit.

He said that this code sometimes doesn’t print anything (I copied exactly what was on the board, I believe this is pseudo-code 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 that you can store in those int variables where none of these conditions will be met ( ____ is not the actual code, obviously, it simply represents that there is something). It does not have to be some int number filling those gaps ... it could be anything in the world (and it could be something that happened before this code).

What can fill these gaps and not lead to any results, and why?

ps - is it somehow related to overflow, undefined behavior, an error outside the bounds, or something like that

pps - I have serious problems with this professor making a mistake. He is more knowledgeable about programming than anyone I have ever met. I am convinced that there are some cases where this is true.

+6
source share
8 answers

All that is needed is one of the int initializations to throw an exception at runtime. Then the program will end before any of the comparative tests. For instance.

 int b = *(int *)0; 
+4
source

I think the important part is _ . If the underlined part contains the results of UNDEFINED code behavior, and the following code with comparisons is optimized by the smart compiler according to UNDEFINED behavior, the final behavior of this code fragment is undefined. And not typing anything is reasonable UNDEFINED behavior.

ps According to Wikipedia, division by zero leads to UNDEFINED behavior, although most compilers determine its error. And the IIRC integer overflow also causes UNDEFINED behavior, although again this usually causes a run-time exception or even a compilation error. Therefore, if a and b are declared as

 int a = 1 / 0; int b = INT_MAX + 1; 

The situation described by your prof. But remember that the behavior is undefined, therefore, regardless of which compiler chooses the program to maintain, it can be considered compatible with the standard.

+2
source

If you have an architecture with int numbers in one addition, there are two zeros: one with all bits set to 0, and one with all bits set to 1, aka positive and negative zero. Both zeros should be compared with equal ones, but the compiler with an error may not notice this :-)

+1
source

For int, this is not true because they do not have "special" values.

0
source

ps - maybe this is due to overflow?

I think this is what your professor had in mind:

 #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; } 

In particular, I use an 8-bit unsigned integer with a fixed width that can hold a maximum of 8 bits (surprise), which represents 256 as the largest possible integer. You can overfill this field by adding to it an amount that cannot be represented in this size, in which case what you left is the flow effect.

There is no real ambiguity in the comparison logic, since this part of the processor (if translated into one of the combinations of the comparison / transition commands). The problem is that you could program it for a reasonable expectation of its work, not realizing that the representations of numbers on a computer have fixed representations. I do not see anything that I could add to those registers that could avoid all “less, more, or equal”.

Note. I used fixed-size integers introduced by C99 for clarity and small sizes so that we can do this in our head.

0
source
 a = 0, b = 5 / a : divide by zero 
0
source

Given that "there may be material that occurred 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; } 
0
source

Maybe defintion print :

 void print(const char*) { } 
0
source

Source: https://habr.com/ru/post/888024/


All Articles