Thread: No unsigned int?

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    No unsigned int?

    Code:
    #include <iostream>
    int main(){
    	unsigned int baa=4294967295;
    	printf("%ui",baa);
    	system("pause");
    }
    I get this warning:

    Code:
    main15.cpp:3: warning: this decimal constant is unsigned only in ISO C90
    Isn't unsigned int a "standard" thing then?

    This code tag script sucks!!!! I couldn't post this thing on the first time (exactly the same thing!!!), then I first posted just "baaaa" and then edited it!!!! Even if I put EVERYTHING inside code tags.

    The code tag checking should not be case-sensitive.
    Last edited by maxorator; 10-13-2006 at 02:22 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Append a 'U' for unsigned, e.g.
    Code:
    unsigned int baa = 4294967295U;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    But the warning still doesn't make sense
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Because unless you add the "U" to the end, a numeric literal is a signed int. And that number can't be represented as a 32 but signed int.

    The compiler does NOT look at the left hand side of that first assignment to determine the type of the right hand side. For a constant, you need to explicitly tell it that the constant should be unsigned.

    In the same way, you couldn't have this:

    f(4294967295);

    because the number will be treated as a signed int, and it can't be represented as a signed int (unless int on your platform is > 32 bits).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But the warning still doesn't make sense
    On a 32-bit system, the largest signed integer is likely to be 2147483647. 4294967295 in two's complement form would then be -1, which clearly is not unsigned.

    You might not get such a warning reported for positive integers less than 2147483647, but that would be because your compiler finds no possible ambiguity on your part (either way the literal is considered unsigned).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    GCC uses already some C99 rules. It is letting you know that you are overflowing the int capacity and that under C99 it will never become unsigned because with that standard constant integers are never unsigned unless adorned.

    Your constant would be a signed long long under C99 and... I think a unsigned long under C90. To fix it place the UL suffix. This will adorn the constant and bypass any compiler interpretation based on the standards.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The compiler does NOT look at the left hand side of that first assignment to determine the type of the right hand side. For a constant, you need to explicitly tell it that the constant should be unsigned.
    This is what I wanted to know.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you want to assign the largest possible value to baa, a portable way is to include <limits> and then use numeric_limits<unsigned int>::max():

    Code:
    #include <iostream>
    #include <limits>
    
    int main() {
      std::cout << std::numeric_limits<unsigned int>::max() << std::endl;
    }

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Or just do ~0U.
    Or (unsigned int) -1.
    Or simmilarly (unsigned int) ~0.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I usually use -1u when I'm feeling lazy; UINT_MAX in <limits.h> when I'm not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Or just do ~0U.
    You can't assume it will work on all platforms. You are not supposed to associate bitwise representation with integral values. For all you know, a machine could be using BCD.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jafet
    You can't assume it will work on all platforms. You are not supposed to associate bitwise representation with integral values. For all you know, a machine could be using BCD.
    Such platforms would be difficult to conform to other requirements of the language. And that is left to the implementors, if they so choose.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by jafet
    You can't assume it will work on all platforms. You are not supposed to associate bitwise representation with integral values. For all you know, a machine could be using BCD.
    You CAN assume it will work with unsigned integral types; the representation is guaranteed to be what you expect. The representation of signed integral types is a little fuzzy so you can't portably right-shift a signed int, for example. I like the C++ numeric_limits method, even though it's more verbose, since it's more general (you can get info for any built-in type) and uniform, so you don't have to look up what the macro is for a given type. And it doesn't matter once the code is compiled since the compiler just substitutes the appropriate constant any way it's done.

    Edit: To be more precise, the action of bitwise operators on an unsigned n-bit integer variable is well-defined (i.e., portable).
    Last edited by robatino; 10-14-2006 at 01:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM