1
#include <iostream>

int main()
{
    int a = 999999999999999999;
    std::cout << a << std::endl;
    std::cout << 999999999999999999 << std::endl;
    return 0;
}

The output of above program is -1486618625 and 999999999999999999.

In both the cout we are giving the same number, so why are the outputs different?

Also are long long int a=999999999999999 same as int a=9999999999999999ll?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Junior
  • 113
  • 1
  • 7
  • 1
    are you sure it also does with long long int? The number 999999999999999 requires 64 bits, so it wint overflow the capacity of long long (on windows for instance), as it did with *int* in your example. – A.S.H Sep 21 '15 at 07:38
  • possible duplicate of [What does the C++ standard state the size of int, long type to be?](http://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be) – Blastfurnace Sep 21 '15 at 08:53

5 Answers5

0

In the first case you are assigning the value to an int, this is too large for an integer where max value is 2147483647, check this link for more info C++ Data Types

in the second case, cout will print whatever value you gave.

Ali Baghdadi
  • 648
  • 1
  • 5
  • 17
0

assigning the constant number 999999999999999999 into an int variable (a) results in truncation and changes the value. However, if you declare it as long long a, there will be no truncation and it displays correctly on any platform that has 64 bits for a long long (i.e. Windows VC++).

A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • _"results in truncation"_. No it does not, it results in overflow. Completely different behaviour. – Andy Brown Sep 21 '15 at 08:09
  • @AndyBrown Here is the warning message from VC++: *...\consoleapplication1.cpp(10): warning C4305: 'initializing' : **truncation** from '__int64' to 'int'* – A.S.H Sep 21 '15 at 08:24
  • 1
    As a stalker, I think this answer doesn't deserve a down-vote. – javaLover Feb 28 '17 at 09:07
  • @javaLover hey! :) as you see, we need to be very careful in choosing the words, the C++ society has a very strict and "templated" vocabulary :P – A.S.H Feb 28 '17 at 09:24
0

When you assign integer literal 999999999999999999 to int a, it may be truncated if the int type is not capable of representing that number. The maximum value that can be stored in an int is std::numeric_limits<int>::max() and depends on the platform you are compiling for (but must be at least 32767). Your compiler should warn you about such a truncating assignment:

$ g++ -std=c++11 -Wall -Wextra    32689548.cpp   -o 32689548
32689548.cpp: In function ‘int main()’:
32689548.cpp:5:13: warning: overflow in implicit constant conversion [-Woverflow]
     int a = 999999999999999999;
             ^

To answer your question, "[is] long long int a=999999999999999 the same as int a=9999999999999999ll" - why not test it for yourself? Like this, perhaps:

#include <iostream>

int main()
{
    long long int a = 999999999999999999;
    int b = 999999999999999999ll;
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << (a==b) << std::endl;
    return 0;
}
$ g++ -std=c++11 -Wall -Wextra    32689548.cpp   -o 32689548
32689548.cpp: In function ‘int main()’:
32689548.cpp:6:13: warning: overflow in implicit constant conversion [-Woverflow]
     int b = 999999999999999999ll;
             ^
$ ./32689548
999999999999999999
-1486618625
0

Here we see that assigning a long long to int b causes the same truncation (as GCC warns us it will). However, if we change int b to auto b, then we get no warnings and a true comparison:

999999999999999999
999999999999999999
1

For information, I built and ran the above on a system that has

std::numeric_limits<int>::max() = 2147483647
std::numeric_limits<long long int>::max() = 9223372036854775807
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • Then what is the use of ll suffix.Is it not used to specify that the integer is long long and then assign that much space for that integer value. – Junior Sep 21 '15 at 12:51
  • @Junior - yes, `ll` indicates that a literal is `long long` - but if you then assign it to `int` you'll get truncation. Just the same as if you write `long long int a = 999999999999999999ll; int b = a`. – Toby Speight Sep 21 '15 at 13:33
0

Variable a is defined as signed integer data type - 4 bytes, and the range value of signed integer is [-2^31, 2^31]. For easy-understanding, value of data type is stored as round circle, so that the minimum value stands next by maximum value [..,max-1, max, min, min+1...]. It means the value of (maximum value + 1) is minimum value. When you do an assignment as int a=999999999999999, because this value is over range value of signed interger data type, a valid value in integer's range value is assigned to the variable a.

Line statement cout<<a<<std::endl; in essence is overloading of std operator basic_ostream<charT,traits>& operator<<(int n) - print out a signed integer value.

Line statement cout<<999999999999999999; is overloading basic_ostream<charT,traits>& operator<<(long n), long data type is 8-bytes data type, value range contains the printed value '99..99', so value '99..99' is printed with no doubt.

Try define var a as long data type long a = 999999999999999999, then printing out will be successful.

  • `long` might not fix the problem, the standard only requires "at least" 32 bits. See the [integer properties table on this page](http://en.cppreference.com/w/cpp/language/types). – Blastfurnace Sep 21 '15 at 08:47
  • In fact, it should be `long long` or `long long int`. I tried compiling on x64 computer, sometimes long is defined as 64-bits data-type. [64 bits computing](https://en.wikipedia.org/wiki/64-bit_computing) – Nhan Nguyen Tri Thanh Sep 21 '15 at 10:04
-2

In the first you are declaring a as a variable of int type. The maximum value of int type is 2147483647 so whatever the value above it will be converted. So your long value was converted and stored and thats what was printed. But when you directly give it without any values, it can be any data type. Here the data type is long long int(64 bit int). So if your compiler, OS and hardware support 64 bit ints, then it will be printed. The first one also will be printed correctly if you declare it as long long int. The maximum value of long long int is 92233703685477807 which is almost enough to store 20! (factorial of 20).

S Praveen Kumar
  • 341
  • 1
  • 13
  • Then what's the use of long int? – S Praveen Kumar Sep 21 '15 at 08:16
  • 1
    There's a [good table on this page](http://en.cppreference.com/w/cpp/language/types) that shows what the language standard says about integer types. As far as `long int` goes, it depends on the compiler and build options. It's at least 32-bits and might be 64-bits. – Blastfurnace Sep 21 '15 at 08:20
  • Ok. Thanks. I changed the value. I was taught to use the Turbo C++ compiler only. I thought that same values apply for all compilers. – S Praveen Kumar Sep 21 '15 at 08:27
  • I kind of assumed you were talking about Turbo C++, it seems to be common in some education systems. It's unfortunate because DOS and Win16 are very old and not seen that much outside that environment. Note: I'm not one of your downvoters, glad you learned something new. – Blastfurnace Sep 21 '15 at 08:31
  • Yeah. Turbo C++ is what was thought in our education system. But GCC and G++ is what I use. Anyway thanks for your info. – S Praveen Kumar Sep 21 '15 at 13:11