-1

Below I have simple C++ code to generate numbers with a length of 18. When I call the gen_nums function, i is equal to 1569325055 as if the number that I assign to the i variable was too big for it.

#include <iostream>

using namespace std;

void gen_nums(){
    int i = 99999999999999999; 
    cout << i <<endl; 
}

int main()
{
   gen_nums();
   return 0;
}
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Sec Team
  • 47
  • 8
  • 4
    Does the compiler say anything about this code when you turn on warnings? – cigien Aug 22 '20 at 14:22
  • Datatypes on a particular architecture have a particular size. The C++ standard makes guarantees about the minimum size of certain types, but not the maximum size. That said, on most modern desktop systems (e.g. not embedded) an integer is 4 bytes. https://en.cppreference.com/w/cpp/language/types – JohnFilleau Aug 22 '20 at 14:23
  • 1
    Please read the answers to [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058) at some point. – Jesper Juhl Aug 22 '20 at 14:24
  • If you `#include ` at the top of your code, you can write `std::cout << "Maximum integer size: " << std::numeric_limits::max() << std::endl;` in `main` to see the largest value an integer can hold on your system. https://en.cppreference.com/w/cpp/types/numeric_limits – JohnFilleau Aug 22 '20 at 14:24
  • *"as if the number that I assign to the `i` variable was too big for it."* -- looks like you already know what's going on. Do you have some reason to doubt your conclusion? – JaMiT Aug 22 '20 at 15:48
  • @JohnFilleau • I should not have mentioned it, I can't talk about it. – Eljay Aug 23 '20 at 22:01

8 Answers8

2

It may be because you're assigning a value to int that is outside of its range.

When you assign an int outside its range, the behavior is implementation defined.

Some compilers truncate the higher bits to the width of the integer type you are assigning it to.

If you really want to use such large numbers, maybe use a long long which has a larger range.

void gen_nums(){
    long long i = 99999999999999999 ; 
    cout << i <<endl; 

    //for (i = 99999999999999999; i < 999999999999999999 ; i++  ){ 
    //    cout << i  << endl; 
      
        
   // }
}

or for convenience, you could just use auto

void gen_nums(){
    auto i = 99999999999999999 ; 
    cout << i <<endl; 

    //for (i = 99999999999999999; i < 999999999999999999 ; i++  ){ 
    //    cout << i  << endl; 
      
        
   // }
}
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
1

99999999999999999 is outside the range of values a int can hold.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
1

Such arbitrary big values can't be held in a 4-bytes integer datatype (if you're using a 64-bit machine). Yet if you're not sure which type is appropriate to fit in it, take the help of auto:

#include <iostream>

void getNums() {
  // 'long int' after type deduction
  auto i = 99999999999999999;
  std::cout << i << std::endl;
}

int main(void) {
  getNums();

  return 0;
}

In my case, the i is deducted into a type of long. It may differ on your system since the architecture may differ.

On the other hand, in case you want to do-it-yourself and see the largest number a 4-bytes (i.e. 32-bits) integer could hold, you could use limits:

void getNums() {
  // 4-bytes integer on 64-bit machine
  // expands to 2,147,483,647 in my 64-bit system
  auto i = std::numeric_limits<int>::max();

  std::cout << i << std::endl;
}

Lastly, consider enabling the compiler warnings. This'll help you a lot.


Referring to a Data Type Ranges if you want to understand better.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
0

The max size of most integers is +2147483647. The number you give to it is too big, you should use "unsigned long long" for example. This is a page with the lengths of data types in c++. It is in German but I hope this is not a problem. http://www.mrknowing.com/2013/10/08/datentypen-in-c-wertebereich-und-speichergroesse/

RandomGuy
  • 23
  • 5
-1

You need to change the integer format you use, int, that in yours as well as in all other implementations has maximum value lower than 99999999999999999, into the long long format, which, as I read, can assume, in some implementations, maximum value of 9,223,372,036,854,775,807.

The value you get of int(99999999999999999) = 1569325055 is the wrong output of internal overflow.

Giogre
  • 1,444
  • 7
  • 19
  • 2
    Where does `1569325055` come from? It should be `2147483647`. – HolyBlackCat Aug 22 '20 at 14:38
  • The OP stated that number is what he gets as output using `int`. – Giogre Aug 22 '20 at 14:41
  • 2
    It doesn't mean that's the max value. If I do something like `int x = 4294967295; std::cout << x << '\n';` I get `-1`, which is surely not the max value. – HolyBlackCat Aug 22 '20 at 14:53
  • In your case, you would have to write `unsigned int x = 4294967295;` to make it work. – Giogre Aug 22 '20 at 15:07
  • I know, that's not the point. That was an example showing that OP's code doesn't necessarily print the max value of `int`. – HolyBlackCat Aug 22 '20 at 15:11
  • Yes, output number would have come out negative if it were in overflow, my bad. Apparently, `1569325055` comes out of typing `long(99999999999999999)`, that is, 17 nines. – Giogre Aug 22 '20 at 15:27
  • Probably this is due to some kind of 2's complement representation quirk. This particular case I find used in C++ exercises, so I am wondering whether the OP is just testing users knowledge here. He shares your same nationality, oddly enough. – Giogre Aug 22 '20 at 15:34
-1

The data type "int" cannot hold that range of value.

Please try this code: using "long int" data type

#include <iostream>

using namespace std;


void gen_nums(){
    long int i = 99999999999999999 ; 
    cout << i <<endl; 

    //for (i = 99999999999999999; i < 999999999999999999 ; i++  ){ 
    //    cout << i  << endl; 
      
        
   // }
}


int main()
{
   gen_nums();
   return 0;
}
-2

You should use int64 instead of int as the number given is outside int range

  • _int8 nSmall; // Declares 8-bit integer
  • _int16 nMedium; // Declares 16-bit integer
  • _int32 nLarge; // Declares 32-bit integer
  • _int64 nHuge; // Declares 64-bit integer
M_K
  • 227
  • 1
  • 14
  • 4
    "You should use int32 instead of int..." - Absolutely *not*. You should *not* use some platform specific types/typedefs when there are perfectly fine [Fixed width integer types](https://en.cppreference.com/w/cpp/types/integer) (like `int64_t`) in the standard that will work across different implementations. – Jesper Juhl Aug 22 '20 at 14:36
-2

The reason for that error is the range of integer in C++ is -2147483648 to 2147483647. So the largest number that you can use is 2147483647.

Abhijith
  • 13
  • 2
  • 5
    The range of `int` in C++ is at least `-32768` to `+32767`, and commonly the numbers you stated. Many embedded architectures still use a 16 bit integer. – JohnFilleau Aug 22 '20 at 14:36
  • The range that you stated above is the range of short int(Typical bit width 2 bytes). The range of integer is -2147483648 to 2147483647(Typical bit width 4 bytes) – Abhijith Aug 22 '20 at 14:44
  • 4
    @Abhijith11151: The C++ standard allows variation in the size and range of the integer types. It is incorrect to assert without qualification that the range for `int` is −2147483648 to 2147483647. Do not teach this wrong information to students. And, when writing about the `int` type, call it `int`, not “integer.” – Eric Postpischil Aug 22 '20 at 14:49
  • 1
    @Abhi a modern example architecture where `int` is still 16 bits is on AVR microcontrollers. These are used in Arduinos, so have a high likelihood of being encountered by beginners. Teaching that `int` is always 32 bits will lead to extreme confusion for these people. It's more precise to say that `int` is commonly 32 bits in modern architectures, but sometimes 16 bits. The standard only guarantees that it is at least 16 bits. – JohnFilleau Aug 22 '20 at 14:52