Thread: integer range

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    integer range

    Code:
    main()
    {
    long int a=2147483647+1;
    printf("%ld",a);
    getch();
    }
    the above program displays an error NUMERIC CONSTANT TOO LARGE....why is it so..? why is it not giving the output -2147483648..?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    I think you preprocessor is calculating the value, not the executable.

    Try this instead (and set optimization options to a very low level)

    Code:
    #include <stdio.h>
    
    int main()
    {
        long int a = 2147483647;
        a += 1;
        printf("%ld", a);
        getch();
        return 0;
    }
    Note that going over the range of valid values for an int is Undefined Behaviour.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Because when you just write some number in your code, that number has a type normally that type is int. When you want it to be a different type then you need to let the compiler know.
    How you do that is to put the appropriate suffix on the value. The suffix for long is L.
    Then you only have to make sure that long is more than 32-bits on your system so that 2147483647L + 1L does not overflow. Otherwise you'd have to use either unsigned long (suffix UL), or long long (suffix LL)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    it certainly unlikely that the program is outputting that error message when you run it. it is a compiler warning, right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using char to store an integer in the range [128,127]
    By nikhil22 in forum C Programming
    Replies: 5
    Last Post: 07-26-2008, 09:44 AM
  2. range of unsigned integer
    By hitesh_best in forum C Programming
    Replies: 2
    Last Post: 12-23-2007, 05:52 AM
  3. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  4. Can yoou return a range of values stored into a integer?
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 04:24 PM
  5. Range of an integer.
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-10-2002, 09:43 AM