3

Why the below code gives integer overflow warning:

#include <stdio.h>

int main()
{
    long long int x = 100000 * 99999;
    return 0;
}

Whereas below code works perfectly:

#include <stdio.h>

int main()
{
    long long int x = 100000000000000;
    return 0;
}
user3243499
  • 2,953
  • 6
  • 33
  • 75
  • 4
    you are multiplying to `int` values (not two `long long` values) – gawi Jul 02 '17 at 19:32
  • 3
    From the [reference for integer literal](http://en.cppreference.com/w/cpp/language/integer_literal): *"The type of the integer literal is the first type in which the value can fit"* - so in this case `int` – UnholySheep Jul 02 '17 at 19:35
  • But in either of the cases, I am just assigning the numbers. It should have been the same result in both cases as the multiplication result is less in the first case than in the second case. – user3243499 Jul 02 '17 at 19:43

3 Answers3

5

Because here

long long int x = 100000 * 99999;

two ints are multiplied. Try

long long int x = 100000LL * 99999;
AlexD
  • 32,156
  • 3
  • 71
  • 65
  • Thanks. I just tried with 1LL * 100000 * 99999 and it worked. But 100000 * 99999 * 1LL did not work. Now I got your point. Thanks. – user3243499 Jul 02 '17 at 19:50
  • 2
    @user3243499 Indeed, because `100000 * 99999 * 1LL` is evaluated as `(100000 * 99999) * 1LL`, and the expression in the brackets is multiplication of two `int`s. – AlexD Jul 02 '17 at 19:52
  • My misconception so far was in C/C++, the final size will always be equal to the highest size in the expression. – user3243499 Jul 02 '17 at 19:58
  • @Ron `const` should not make any difference here. – AlexD Jul 02 '17 at 19:58
  • @user3243499: Indeed, just for the record, integer promotion (of smaller operands to match wider ones, or of both to int if narrower) happens during evaluation of each binary operator separately, in order of operator precedence. – Peter Cordes Feb 25 '21 at 08:39
1

Make it 100000LL * 99999LL for the warning to go away.

Ron
  • 14,674
  • 4
  • 34
  • 47
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
0

You should read this or this.

The type of the integer literal is the first type in which the value can fit, from the list of types which depends on which numeric base and which integer-suffix was used.

(Thanks to @UnHolySheep who makes me notice it). If you want the compiler to interpret differently your literal, you have to add a suffix. For example, with int, you can add LL or ll to specify that it's a long long int. With unsigned numbers the suffix is u.

With floating point literals, it's the same: there's a default type, which is double, but if you want a float you can easily use the f (or f) suffix. With floating point you can even use exponential rappresentation (usign the e).

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42
  • 2
    *"By default integer literals (constants) are of type int"* - No, did you read the second link yourself? Integer constants or of the first type that can hold them, it just happens to be `int` in OP's case. If the number is sufficiently large to not be able to be held in an `int` the next bigger type is chosen (and if that cannot hold it either then the next bigger one, etc.) – UnholySheep Jul 02 '17 at 20:08