long integer

hello,
i have made a program to try the "long integer" instead of integer for a very long number, but it has some really strange results. Is it possible to look for this problem ? Many thanks

// eventjes testen wat we nog weten van gisteren
#include <iostream>
using namespace std ;
int main (){
cout << "geef een variabele"<< endl;
string var1 ;
cin >> var1;
cout << "geef de string weer "<< var1 << endl ;
cout << "geef een integer "<< endl;
long int var2 ;
cin >> var2;
cout << "integer is "<< var2 << endl;
cout << "de hoeveelheid bytes is"<< sizeof(var2)<< endl;

return 0;
}


This came out of result :

sdgsfgs.
geef de string weer sdgsfgs.
geef een integer
5469484949864565465
integer is 2147483647
de hoeveelheid bytes is4

I don't understand why he didn't gave me this very long number to me ...

Many thanks
dries
You are presumably on a 32-bit system and your compiler has chosen a size of 32 bits for long int. It could have chosen 64 bits and certainly would have on a 64-bit system. The value 2147483647 is the maximum value for a 32-bit signed int. You can try long long int, which is guaranteed to be 64-bits.
Last edited on
https://en.cppreference.com/w/cpp/language/types
Scroll down to "Range of values"
You would need to use "long long int" to represent such a number.

You can use :
int64_t var1; to get a 64-bit int, or uint64_t var1; to get an unsigned one.
Topic archived. No new replies allowed.