Why is int variable displaying out of range values

Discussion in 'C' started by sparemejava, May 11, 2010.

  1. sparemejava

    sparemejava New Member

    Joined:
    May 11, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Please see the below code and help me understand why an out of range value is displayed here.

    #include<conio.h>
    #include<stdio.h>

    main()
    {
    int sum=32768;
    printf("%d",sum);
    }

    output = 32768

    Since sum is an integer, its range is -32767 to 32768 and it is not defined as an unsigned integer. But why the output is 32768.
     
  2. cb92

    cb92 New Member

    Joined:
    Jul 28, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    What is your OS?
    A signed integer occupies 2 bytes of memory in Windows.
    Another thing, the correct range is: -32768 to 32767. (It only seems to be a typing mistake according to your question.)
     
  3. Mohammad Shoaib

    Mohammad Shoaib New Member

    Joined:
    Jan 29, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    It is known that different application software allow different ranges of the data type int. There are application software, in which the range is -32768 to 32767, and in some, is -2148473648 to +2148473647. From my point of view, your application software has the range of 4bytes, and therefore, showing you the result that you consider wrong, but is true actually. To check, change the value of your variable into 2148473678. You will get your answer.

    Hoping you got your answer.
    Thanks
     
  4. Mohammad Shoaib

    Mohammad Shoaib New Member

    Joined:
    Jan 29, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Answer: I've sent you the answer but by mistake, I wrote 2148473647, which is 2147483647. Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice