signed int
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    bash64
    Posts: 958 from 2010/10/28
    From: USA
    I would prefer to have the compiler assume all integers as being unsigned.
    However, what do most C calls expect? signed or unsigned values?
    If unsigned then I don't quite get having signed values as the default and having to manually type unsigned before each variable.
    Programming languages are supposed to take the work out of programming, not add to it.
    Any issues with me telling the compiler to assume unsigned integers?
    Mac G5 ISight 21" 2.5 gb of ram 233gb hd matshita dvd-r uj-846
    Powerbook G4 1.67ghz 2GB, ATI 9700M Pro 128mb
    1TB hd, DL-DVD Burner, Netgear pcmcia wireless card.
    ImageFX 4.5, PageStream 3.3, PhotoGenics 5.0
  • »12.05.13 - 18:23
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    Tcheko
    Posts: 538 from 2003/2/25
    From: France
    The compiler assumes nothing about signed/unsigned : this is all about you to define the type you want be it signed or unsigned.

    If you use exec/types.h to define your vars, there is a one char overhead for unsigned.

    Use ULONG instead of unsigned long for example.

    C function calls expect the type defined and nothing else. For OS calls, look at prototypes.

    If you use Scribble editor, there is hint for function arguments.

    About GCC, there is a switch for signed/unsigned char. See signed/unsigned chars
    Quelque soit le chemin que tu prendras dans la vie, sache que tu auras des ampoules aux pieds.
    -------
    I need to practice my Kung Fu.
  • »13.05.13 - 07:51
    Profile Visit Website
  • Paladin of the Pegasos
    Paladin of the Pegasos
    Jupp3
    Posts: 1193 from 2003/2/24
    From: Helsinki, Finland
    Both signed and unsigned values have their uses. If you don't need negative numbers, it feels "natural" to use unsigned, but then again, with int, you lose "only" half of the range, which usually isn't really critical.

    Even if unsigned int does feel more natural, sometimes signed still has some benefits. Consider following code:
    unsigned int a=b-c-d-e;

    As such, there's no issue. BUT if it's possible for a to end up negative (in which case, it's not processed at all) things can become more complex.

    You can write something like:
    a=b;
    if(a>=c) a-=c; else break;
    if(a>=d) a-=d; else break;
    if(a>=e) a-=e; else break;
    DoSomething(a);

    However, this becomes MUCH more simple with signed:
    int a=b-c-d-e;
    if(a>=0) DoSomething(a); // Safe signed => unsigned cast

    Also, unsigned values can be often used for "special cases" (errors etc.) when all "valid" values are positive.

    But whenever using functions, it's a bit faster, if your variable types match those of arguments & return values.
  • »13.05.13 - 13:39
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    bash64
    Posts: 958 from 2010/10/28
    From: USA
    thanks
    Amazing I understand everything you just said.
    I spent an hour reading C - The complete reference and picked up a ton of info.
    The dummies book is too low on the totem pole for me.
    Mac G5 ISight 21" 2.5 gb of ram 233gb hd matshita dvd-r uj-846
    Powerbook G4 1.67ghz 2GB, ATI 9700M Pro 128mb
    1TB hd, DL-DVD Burner, Netgear pcmcia wireless card.
    ImageFX 4.5, PageStream 3.3, PhotoGenics 5.0
  • »13.05.13 - 14:44
    Profile Visit Website
  • Order of the Butterfly
    Order of the Butterfly
    Tom01
    Posts: 182 from 2009/9/20
    Don't use unsigned variables.
    On a 32 bit cpu unsigned variables are superfluous.
  • »13.05.13 - 21:15
    Profile Visit Website