portable declarations

This is a discussion on portable declarations within the C Programming forums, part of the General Programming Boards category; Hi everyone Where can I find a list of portable declarations for ints and floats?...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    17

    portable declarations

    Hi everyone

    Where can I find a list of portable declarations for ints and floats?

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    What's a portable declaration?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    Some compliers, I am told, regard an int as 16 bits others as 32 bits. There are portable
    means of declaring variables and I can't recall what they are (haven't been C coding for a while).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I do not think there is such a list. There are minimum sizes specified, but even for char the size in terms of bits is not fixed other than for the minimum. Still, you could use sizeof and CHAR_BIT to find out.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The stuff in limits.h will tell you what your compiler supports in terms of maximum and minimum values, but most of the time you don't have to worry about how many bits a type has. For example, if you need more than 32,767 or less than -32,767, use a long int instead of an int because it has a guaranteed minimum of 32 bits and an int only has a minimum of 16 bits. If you have to know how many bits a type has, you probably need to be using an array of char because your stuff is really low level.

    Some compilers support portable types like int32 and int16, but ironically, they're not portable.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    stdint.h has a list of types such as int32_t, but again isn't completely portable.
    If you understand what you're doing, you're not learning anything.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Cool, I missed that. Though it seems that, in C99:
    These types (Exact-width integer types) are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, it shall define the corresponding typedef names.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by Noir View Post
    The stuff in limits.h will tell you what your compiler supports in terms of maximum and minimum values, but most of the time you don't have to worry about how many bits a type has. For example, if you need more than 32,767 or less than -32,767, use a long int instead of an int because it has a guaranteed minimum of 32 bits and an int only has a minimum of 16 bits. If you have to know how many bits a type has, you probably need to be using an array of char because your stuff is really low level.

    Some compilers support portable types like int32 and int16, but ironically, they're not portable.
    The equivalent header file for floating point variables types is <float.h>.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    Itsme86 wrote:

    stdint.h has a list of types such as int32_t, but again isn't completely portable.
    What indication do you have that the declarations as given by stdint.h wont be
    completely portable?

    Thanks

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you even read laserlight's reply? Why bother making a thread if you won't read what people write in it?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    Thanks all
    The number of assertions that can not be disproven are only limited by one's imagination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 02:03 PM
  2. mixing code with declarations
    By robwhit in forum C Programming
    Replies: 12
    Last Post: 01-30-2008, 11:55 PM
  3. C - Portable?
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 07-15-2007, 09:09 AM
  4. help on declarations
    By robasc in forum C Programming
    Replies: 9
    Last Post: 03-05-2005, 12:50 PM
  5. which Portable mp3 player?
    By Raihana in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 01-09-2004, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21