What is “int I = 1;Why (i >= 60 * 60 * 1000 / 1 * 1000)” true?

All operators on int s return int So yes 60 * 60 * 1000 / 1 * 1000 - 1 is an int But the expected result of 3599999999 is too big for an int so the expression actually evaluates to -694967297 (assuming 32-bit int and two's complement).

All operators on ints return int. So yes, 60 * 60 * 1000 / 1 * 1000 - 1 is an int. But the expected result of 3599999999 is too big for an int, so the expression actually evaluates to -694967297 (assuming 32-bit int and two's complement).

This doesn't happen with a literal 3600000000 because integer literals larger than INT_MAX are of a type that can hold the full value.

20 "a type that can hold the full value" - IF such a type exists. There's no guarantee for literals that exceed ULONG_MAX. – MSalters Jul 14 '11 at 8:56 2 Integer (as opposed to stack) overflow :) – Dark Star1 Jul 19 '11 at 18:39 Even 60 * 60 * 1000 may be too big for an int; seem my answer.

– Keith Thompson Aug 7 '11 at 0:09.

60 * 60 * 1000 / 1 * 1000 - 1 = 3600000 * 1000 - 1, which overflows the int type, so the result can be anything (in your case it's negative, but it doesn't have to be). To achieve what you want put ( ): #define BIG_INTERVAL (60 * 60 * 1000) #define SMALL_INTERVAL (1 * 1000).

13 +1 for suggesting parentheses - that explains the real issue - though avoiding macros would be even better advice. Still, "overflows the int type, so it's negative" makes this sound like the former always implies the latter. Pedantic, but formally it's undefined behaviour for signed integral types and in practice it generally means you get garbage that might be positive or negative... here 3,600,000 * 1,000 happens to be 2^31 so it's negative on common platforms with 32-bit ints.... – Tony Delroy Jul 14 '11 at 6:27 thanks, I made it a little more clear – Petar Ivanov Jul 14 '11 at 6:39.

Here's my test results: 60 * 60 * 1000 / 1 * 1000 will result to -694967296 (60 * 60 * 1000) / (1*1000) will result to 3600 There's a problem with your operation, the precedence of computations. You might want to consider looking at the C++ operator precedence msdn.microsoft.com/en-us/library/126fe14.... You'll find the reason why the result became -694967296 which I think effect of overflow.

Muntoo - Here, I explained it. – domanokz Jul 14 '11 at 6:40.

If you use a compiler where int is 64 bits, you will find that the result of your expression is false. If you use a compiler where int is 32 bits or 16 bits, your expression has undefined behaviour because overflow of signed ints doesn't have to wrap around. Probably yours did just wrap around, but it doesn't have to.3600000000 is a constant visible at compile time, so if int is only 32 bits then your compiler will have to choose long long (or just long if long is 64 bits).

So your other expression is evaluated with enough bits to avoid overflowing, and the result is correct.

– mafutrct Jul 14 '11 at 10:26 1 @mafutrct: C and C++ standards do not mandate anything on the representation of signed types, so there is no guarantee that you'll be wrapping around since 2-complement is not compulsory (and overflow of operations is undefined). For n-bit signed types, however, the standards mandates modulo 2^n arithmetic. – Alexandre C.

Jul 14 '11 at 12:02 what compiler do you know of where int is 64 bits? – Dov Jul 14 '11 at 13:29 1 @Alexandre C. You probably mean "For n-bit unsigned types, however, ..." – luiscubal Jul 14 '11 at 13:49 @luiscubal: yes, of course.

Too late for editing though. – Alexandre C. Jul 14 '11 at 14:05.

Could be that you're overflowing the size of an int which is 2147m or so signed which means if you go over the representation for that becomes negative. As pointed out by other answers the division does nothing when expanded so surround the macro definitions with parentheses.

Approx. 2147 million , not 2.7 million (assuming 4 byte int) – Mitch Wheat Jul 14 '11 at 6:19 Thanks, I really shouldn't answer questions while sleepy :P – Jesus Ramos Jul 14 '11 at 6:20 1 It might be of interest to note that this probably happens for the OP because the internal representation uses two's complement, but overflow behavior in general is undefined. – Maxpm Jul 14 '11 at 6:21 @Maxpm you are completely correct as some compilers actually handle overflow of constant values by assigning it to -1 or some other value – Jesus Ramos Jul 14 '11 at 6:23.

You are most likely going outside the valid range of values for a signed int - 3600000000 is quite a large number! When this happens, the value will become the smallest negative value for the int data type. This will result in your statement being true.

Each of the arguments to that expression is an integer, so the result will be an integer.

Yes, they're integers. More to the point, they're ints. (The word integer covers a number of types, from char up to long long; int' is a particular type, not *just* an abbreviation of integer`.) – Keith Thompson Aug 7 '11 at 0:02.

I think you are confused about how Macros work. You are not using the values from those macros but the equations themselves. I think this is were your confusion lies.

I think you should parenthesis in your macros or not use macros.

Something that I haven't seen anyone mention is that even fully parenthesizing the macro definitions doesn't completely solve the problem. The question has: #define BIG_INTERVAL 60 * 60 * 1000 (and the questioner acknowledges that the lack of parentheses is a problem). But even with: #define BIG_INTERVAL (60 * 60 * 1000) each of the constants (60, 60, and 1000) is definitely representable as an int, but the product is 3600000, whereas the language only guarantees that INT_MAX >= 32767.

The language says that large integer constants are of a type big enough to hold their values (for example, 100000 can be either of type int or of type long int, depending on the ranges of those types), but it has no such rule for expressions, even constant expressions. You can work around this like this: #define BIG_INTERVAL (60L * 60L * 1000L) but that makes it of type long even if it doesn't need to be. As for the operator precedence issue, here's my favorite example: #include #define SIX 1+5 #define NINE 8+1 int main(void) { printf("%d * %d = %d\n", SIX, NINE, SIX * NINE); return 0; } The output, of course, is 6 * 9 = 42 (see Douglas Adams).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions