2

I wrote program from C++ Primary Plus about sizes of variables here's the code :

#include <iostream>
#include <climits>
using namespace std;

int main() {
    int n_int = INT_MAX;
    short n_short = SHRT_MAX;
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;

    // wielkosc
    cout << "int is " << sizeof (int) << " bytes." << endl;
    cout << "short is " << sizeof n_short << " bytes."<< endl;
    cout << "long is " << sizeof n_long << " bytes." << endl;
    cout << "long long is " << sizeof n_llong << " bytes." << endl;
    cout << endl;

    cout << "Maximum values: "<< endl;
    cout << "int: " << n_int << endl;
    cout << "short: " << n_short << endl;
    cout << "long: " << n_long << endl;
    cout << "long long: " << n_llong<< endl << endl;
    cout << "Minimum int value = " << INT_MIN << endl;
    cout << "Bits per byte = " << CHAR_BIT << endl;
    return 0;
}

When I compile it by Cygwin in Eclipse IDE and run this tell me that LONG_MAX = 9223372036854775807, which is not true, I did something wrong? Thanks.

Actual output :

int is 4 bytes.
short is 2 bytes.
long is 8 bytes.
long long is 8 bytes.

Maximum values: 
int: 2147483647
short: 32767
long: 9223372036854775807
long long: 9223372036854775807

Minimum int value = -2147483648
Bits per byte = 8

Expected output:

int is 4 bytes.
short is 2 bytes.
long is 8 bytes.
long long is 8 bytes.

Maximum values: 
int: 2147483647
short: 32767
long: 2 147 483 647
long long: 9223372036854775807

Minimum int value = -2147483648
Bits per byte = 8
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Hornise
  • 21
  • 5
  • 1
    Are you on a 64-bit system? Have you checked e.g. `sizeof(long) `? – Some programmer dude May 17 '15 at 15:50
  • 1
    @Hornise. Why do you say that is incorrect? What's the result you expect? – Sir Jo Black May 17 '15 at 15:51
  • Also, if you're programming C++ you might want to use [`std::numeric_limits`](http://en.cppreference.com/w/cpp/types/numeric_limits) instead. – Some programmer dude May 17 '15 at 15:52
  • Yes, I did, nothing change. – Hornise May 17 '15 at 15:52
  • 2
    And can you please edit your question to include the *actual* and *expected* output? Most importantly, what *is* `sizeof(long)`? – Some programmer dude May 17 '15 at 15:53
  • I expect that LONG_MAX = 2 147 483 647 – Hornise May 17 '15 at 15:53
  • Why do you expect that? – Mats Petersson May 17 '15 at 15:54
  • @Hornise that is true on 32 bit environment, on 64 bit is 9223372036854775807. Try to execute: `printf("%lld %d\n",LONG_MAX,sizeof(long));` and look at the result! – Sir Jo Black May 17 '15 at 15:55
  • I suspect the confusion comes because using the Microsoft Visual C++ compiler, `long` is still 32 bits even on 64-bit systems and builds, while GCC (used by Cygwin) have `long` being a 64-bit type. And if you check the output, that's what it says: "long is 8 bytes" (though you say that you *expect* it to be 8 bytes, when you really expect it to be 4). – Some programmer dude May 17 '15 at 15:56
  • Well, either you should expect size of `long` to be 4, or expect a 64-bit `MAX_LONG` - so clearly your expectations are inconsistent, since you are expecting 8 bytes for size of `long`, but a 32-bit max-value. – Mats Petersson May 17 '15 at 16:00
  • As for why the two compilers can have different sizes, it's because that's how the C++ specification is written. It basically only says `sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)`. So `int` and `long` *may* be the same size, but `long` and `long long` may *also* be of the same size. – Some programmer dude May 17 '15 at 16:00
  • @JoachimPileborg. I often think that should be better the standard make a choise on these behaviours. This situation creates confusion. On the other hand, I understand, that "stdint.h" defines types clearer. – Sir Jo Black May 17 '15 at 16:00
  • Thanks @JoachimPileborg Your explanation helps me alot to understand it :) – Hornise May 17 '15 at 16:07

1 Answers1

2

Why are you so sure LONG_MAX != 9223372036854775807? That seems good to me, as 2^63 -1 == 9223372036854775807. That means that longs are 64 bits on your implementation, with the first bit used as a sign bit. Remember the Standard doesn't specify exactly how big all the different data types should be - on Visual Studio's compiler, long is the same size as int, which is a bit annoying ;)

Robin Hartland
  • 336
  • 2
  • 12
  • Alright ! Thanks ! Sorry for my stupid question but this is my day two with programming and I dont know how something work, one more time: thanks and have a good day :D – Hornise May 17 '15 at 16:04
  • Anyway, what do you think about C++ Primary Plus Stephan Prata? – Hornise May 17 '15 at 16:04
  • 1
    @Hornise See e.g. [this](http://www.reddit.com/r/learnprogramming/comments/1ba00q/should_i_use_c_primer_plus_6th_ed/), And also [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Some programmer dude May 17 '15 at 16:09
  • I have personally got a lot out of Bruce Eckel's "Thinking in C++", which is mentioned in The Definitive C++ Book Guide and List (linked in Joachim Pileborg's comment). You can download it for free! :) – Robin Hartland May 17 '15 at 16:54