Using sizeof( long ) at compile time?

This is a discussion on Using sizeof( long ) at compile time? within the C Programming forums, part of the General Programming Boards category; Originally Posted by novacain Looked at the #error preprocessor command? Something like #if sizeof(long) != 4 #error Long incorrect size! ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by novacain View Post
    Looked at the #error preprocessor command?

    Something like

    #if sizeof(long) != 4
    #error Long incorrect size!
    #endif
    That was how this started, and the preprocessor can't resolve "sizeof(long)" - it's something the compiler proper needs to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Maybe I should just do this:
    Code:
    #if UINT_MAX != 0xFFFFFFFF
    #  error "ints aren't 4 bytes here!"
    #endif
    Yeah, or, if you know how many bits you want in an unsigned int
    Code:
    #if UINT_MAX != (1 << 32) - 1
    #error sizeof(unsigned int) != 4
    #endif
    or, if you have the number of "characters" you want an unsigned int to be,
    Code:
    #if UINT_MAX != (1 << (4 * CHAR_BIT)) - 1
    #error sizeof(unsigned int) != 4
    #endif
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  3. Farmer JoeBot - first project in a LONG time
    By jdinger in forum Game Programming
    Replies: 6
    Last Post: 09-16-2004, 08:57 AM
  4. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM

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