Thread: Visual Studio 7

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    2
    Rep Power
    0

    Visual Studio 7


    Hi!

    Why does this code not work?



    const int INTSIZE = sizeof(int); //sizeof(int) = 4

    #if (INTSIZE!=4)
    //do something
    #endif



    INTSIZE is 0!!! in the #if statment above, why??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    INTSIZE is an integer variable that will only exist when the code is running. #if is a compile time only test. Since INTSIZE has no value at compile time, it will evaluate to zero. If you really want to do a one time capture of sizeof(int) you can try this instead:

    #define INTSIZE (sizeof(int))

    Then your #if will work.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Right Coast
    Posts
    25
    Rep Power
    0
    The "#" is for conditional compilation, so just test with a normal if

    if( INT_SIZE !+ 4){

    //do stuff

    }

IMN logo majestic logo threadwatch logo seochat tools logo