Ask your own question, for FREE!
Computer Science 44 Online
OpenStudy (anonymous):

What is the behavior of the following C code? #include #include int main (void) { printf ("%d\n", (INT_MAX+1) < 0); return 0; }

OpenStudy (anonymous):

INT_MAX is maximum integer if you add + 1 to it, it becomes minimum integer -2^31 so it is smaller than 0 and it prints 1 which is true

OpenStudy (anonymous):

Isn't it undefined behavior?

OpenStudy (anonymous):

it's like that in many programming languages, so probably defined behaviour because they made it to happen like that, if they wanted they could make an error

OpenStudy (anonymous):

http://blog.regehr.org/archives/213

OpenStudy (anonymous):

i dont' really know what does undefined behaviour means :D

OpenStudy (espex):

Undefined behavior is just that, behavior that is not explicitly defined and as such does not have a specific way to be handled.

OpenStudy (anonymous):

well you kinda can to handle this, always when adding check MAX_INT-number<numberwhichisbeingadded and same with multiplying :D

OpenStudy (anonymous):

That's the thing: in C the compiler doesn't have to perform such a check (especially if I'm using an optimizing compiler).

OpenStudy (anonymous):

yeah compiler doesn't but you can :D in java same thing happends

OpenStudy (farmdawgnation):

I could go into the technical reasons why that happens if you want, but only if you really want to get into some serious discussion over the architecture behind representing computers in memory. With regard to the C compiler not checking for things like that - it really shouldn't. This is what developers would classify as a runtime issue. The compiler's job is only to alert about syntax issues. C assumes you know what you're doing with the semantics of your program. I can attest from my personal experience that the compiler assuming the developer knows what he's doing isn't always a good idea. Haha.

OpenStudy (anonymous):

why this is allowed in JAVA too?

OpenStudy (farmdawgnation):

Well, the fact of the matter is that Java - while it makes *fewer* assumptions - will tend to think you know what you're doing. That's not as big of a deal in Java because the JVM provides a number of protections that prevent you from doing anything too tragic. C provides no such protections. In short, there are lots of situations where being able to do arithmetic like this is useful. For example, calculating checksums when you're working on a network program that uses four-byte checksums in its messages. What occasionally happens in practice is that the number in the checksum will get so large that it goes past the maximum Integer. Since we've dictated that checksums MUST be four bytes (for our fictional protocol), we can't just decide to start using longs. So we keep adding and roll back over to zero. In the end, the checksum still serves its purpose and everyone is happy.

OpenStudy (farmdawgnation):

Of course, in C these checksums would be unsigned and would roll over to zero. In Java they would roll back over to the lowest negative number. End result is the same.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
midnightriver46: Is anyone learning korean in here?
36 minutes ago 18 Replies 1 Medal
Gucchi: physics
21 hours ago 17 Replies 0 Medals
Gucchi: physics
1 day ago 10 Replies 0 Medals
gelphielvr: What's the difference between colonization and imperialism
20 hours ago 2 Replies 0 Medals
gelphielvr: I need tips on how to memorize long formulas does anyone have any
4 hours ago 4 Replies 0 Medals
gelphielvr: how do I find the valance electrons? no further info
4 hours ago 1 Reply 0 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!