Thread: Confusing!!!

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Question Confusing!!!

    Hi all,
    Presently I am learning c.
    I am surprised and confused by the following code out put.I am using 32 bit machine, cc compiler.
    Code:
    #include <stdio.h>
    
    int main ()
    {
       int x = 2147483648;
       printf ( "x = %d\n", x );
    
    return 0;
    }//main
    I am getting the following error
    "decimal constant is so large that it is unassigned"

    Then i tried with different options like unassigned int x, long int x, long x,
    every time I am getting the same error message!

    Lastly i tried with double x = 2147483648;
    I am getting same error message??But when I add zero /zeros I am getting the out pot.
    for example if I give like double x = 214748364800;
    i am getting out put.

    Please note that if i give x = 2147483647; (only diffrence is last digit), I am getting the correct out put in the different data types?

    What was the wrong with it?

    Regards
    Chandu

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The problem is the literal, not the variable type. You need to tell the compiler that the LITERAL VALUE is unsigned. You do that by appending the letter "u" to it:

    Code:
    int x = 2147483648u;
    And by the way, it's just a warning, not an error message.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What was the wrong with it?
    On your machine the upper limit of a signed integer is (2^31)-1. That's 2147483647. When you say 2147483648, you're exceeding the bounds of a signed integer, and the compiler is telling you that. Since integer literals are signed by default, this is a valid message. You can remove the warning by saying 2147483648U to mark the literal as unsigned.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it's not "unassigned int", it's unsigned int.

    and a question: since the literal is unsigned, wouldn't the variable have to be unsigned also?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robwhit View Post
    it's not "unassigned int", it's unsigned int.

    and a question: since the literal is unsigned, wouldn't the variable have to be unsigned also?
    Nope. In fact, with gcc and all warnings turned on, it doesn't even warn. The compiler just copies the bit pattern into the variable with no regard to signedness. A warning might have been nice.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >since the literal is unsigned, wouldn't the variable have to be unsigned also?
    It doesn't have to be, but it's certainly a good idea if the value exceeds a signed type because you've suddenly entered undefined-like implementation-defined behavior. If that makes sense.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    brewbuck: yeah... that's weird. Prelude: it does

    which reminds me.... make sure to use &#37;u for unsigned ints.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    Thank you for your kind replies. If I use the following.

    int x = 2147483648u;
    Now there are no warning message.But out put is with - sign (-2147483648)? I was expecting 2147483648.

    In c do we need to declare both variables and literals? or only variables?
    what is the difference between the following?

    unsigned int x = 2147483648;
    unsigned int x = 2147483648u;
    int x = 2147483648u;


    Regards
    Chandu

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Did you print it with the &#37;u format specifer as the post right above yours says?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    unsigned int x = 2147483648; //integer constant is too big
    unsigned int x = 2147483648u; //unsigned integer constant is stored in the unsigned var
    int x = 2147483648u; //unsigned constant is stored in the signed integer var
    //because the constant is too big to fit the integer conatant as is - some implementation defined behavior applies (2147483648u is converted to -2147483648 in your case since on the compiler used they have the same bit representation)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jun 2007
    Location
    Rome, NY
    Posts
    24
    Code:
    #include <stdio.h>
    
    int main() {
            unsigned int x = 2147483648u;
            printf("x = &#37;u\n", x);
            return 0;
    }
    I ran the above on my Solaris machine and got the following:

    jaws% gcc -o test test.c
    jaws% ./test
    x = 2147483648

    So, the above posts are correct. I was just curious about the value myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf is confusing me.
    By babelosopher in forum C Programming
    Replies: 10
    Last Post: 07-12-2007, 04:22 PM
  2. Most confusing language
    By Suchy in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-22-2007, 09:08 PM
  3. confusing error messages?
    By ssjnamek in forum C Programming
    Replies: 6
    Last Post: 01-26-2006, 08:56 PM
  4. Arrays are confusing...
    By GavinHawk in forum C Programming
    Replies: 10
    Last Post: 11-29-2005, 01:09 AM
  5. Confusing Pointer
    By loko in forum C Programming
    Replies: 4
    Last Post: 08-29-2005, 08:52 PM