Thread: portable declarations

  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
    28,413
    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.
    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

  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,218
    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
    28,413
    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.
    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

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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-31-2008, 12:55 AM
  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, 01:50 PM
  5. which Portable mp3 player?
    By Raihana in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 01-09-2004, 07:58 AM