Menu

#58 short vs. int

closed
None
5
2007-02-07
2007-02-07
Ron
No

I do not have access to the formal ANSI C standard.

But I have found on the internet that some compilers consider int to be 16 bit and other compilers consider int to be 32 bit. Short is always 16 bit.

What I have been told is that short is indeed formally 16 bit.

Int however depends on the bus width of the traget processor.

I you use a 16 bit processor the compiler should formally use int as being 16 bit. If you use a 32 bit processor the compiler should formally use int as being 32 bit.

This should also mean that for the processors I use professionally, the PIC16 and PIC18 processors that int should be 8 bit!

By making int dependent on the processor core width, I can imagine that it is possible to write processor independent code without know the processor bus width where the code is automaticaly optimized for the bus width.

Because many programmers use int as 16 bit values I would like to propose a compiler option to activiate a formal int option.

Discussion

  • Bernhard Held

    Bernhard Held - 2007-02-07
    • assigned_to: nobody --> bernhardheld
    • status: open --> closed
     
  • Bernhard Held

    Bernhard Held - 2007-02-07

    Logged In: YES
    user_id=203539
    Originator: NO

    The standard says that sizeof(short) is 2 and sizeof(short) <= sizeof(int) <= sizeof(long).
    Our goal is to make SDCC as compliant as possible, therefore we stick to sizeof(int) = 2. A change would have a huge impact on the source, because (among others) all the promotion logic has to be adjusted. The chance of realisation of your feature request is very, very near to 0.

    > By making int dependent on the processor core
    > width, I can imagine that it is possible to
    > write processor independent code
    Because of the very limited number range of a 8 bit variable you will get many troubles when porting source to a compiler with sizeof(int) == 1. It's common practice to use typedef's to create own types. By using '#if defined(SDCC)' you can make them compiler dependant.

     
  • Maarten Brock

    Maarten Brock - 2007-02-07

    Logged In: YES
    user_id=888171
    Originator: NO

    And to make it complete there is also <stdint.h> which defines many different integer types of exact specified size.

     

Log in to post a comment.