Help for C++ beginner? Cannot declare long int

1    16 Aug 2015 06:22 by u/ROCKduhHOUSE

Hi, I started learning C++ earlier this week. Today I learned about variable types such as integers and floats, but one part the instructor did that I couldn't is make a long int. I am using Code::Blocks with GNU GCC Compiler, whereas my instructor is using Eclipse with MinGW if I am not mistaken.

Declaring a long int didn't seem to change the size of the numbers I could input. I even used tips he gave to show whether it was working or not. Here is the code:

Edit: For clarification, I write comments above the lines that I am referring to except for the last comment.

#include <iostream>
//allows use of INT_MAX and INT_MIN
#include <limits.h>
using namespace std;
int main()
{
    //Declare integer variable "value"
    int value = 47283;
    cout << value << endl;
    //Should output the max value an integer can hold
    cout << "Maximum integer value: " << INT_MAX << endl;
    //Should output the min value an integer can hold
    cout << "Minimum integer value: " << INT_MIN << endl;
    //Is supposed to declare a long integer. I gave it a value 1 above INT_MAX for testing purposes.
    long int absurdValue = 2147483648;
    cout << endl << "Longer integer: " << absurdValue << endl;
    //Should tell the number of bytes of memory used by an integer.
    cout << "Size of integer: " << sizeof(int) << endl;
    //Should tell the number of bytes of memory used by a long integer.
    cout << "Size of long int: " << sizeof(long int) << endl;
    //Both output the same number of bytes?
    return 0;
}

And here's what it output:

47283
Maximum integer value: 2147483647
Minimum integer value: -2147483648
Longer integer: -2147483648
Size of integer: 4
Size of long int: 4

Notice how even when I choose a value just one above the max for a regular integer, it goes negative as though I had declared a regular integer instead of a long int. Also notice that when I use sizeof(long int) and sizeof(int), they both return 4, meaning 4 bytes. Am I using incorrect syntax or declarations for my compiler?

3 comments