Thread: #define behaviour

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    #define behaviour

    Hi,

    Can any body explain me the output of following:

    Code:
    #include<stdio.h>
    int array[]={1,2,3,4,5,6,7,8};
    #define SIZE (sizeof(array)/sizeof(int))
    
    int main()
    {
    printf("%d",SIZE);
    if(-1<=SIZE) printf("1");
    else printf("2");
    return 0;
    }

    8
    2
    SIZE is greater than -1 so it should print 2. It's working fine with positive numbers.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Do you know what type SIZE is?
    Do you know how the compiler resolves comparisons between signed and unsigned types?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    SIZE will always be >= 0 (it's unsigned)

    A sizeof expression evaluates to an unsigned value equal to the size in bytes of the "argument" datatype
    Last edited by zacs7; 05-29-2008 at 10:48 PM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks for reply.........
    But if it >=0 then why 1 is printed ?
    Please explain it in more details ......

    Thanks a lot..

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You obviously didn't answer question #2
    Quote Originally Posted by Salem
    Do you know how the compiler resolves comparisons between signed and unsigned types?
    Remember, when unsigned values go below 0 they loop back to their maximum value, and so on.

    Try the following (assuming char has the range -128 to 127, unsigned char 0 to 255):

    Code:
    unsigned char i = 0;
    for(i = 0; i < 512; i++)
    {
        printf("&#37;d\n, i);
    }
    You'll probably see it hit 255 and go back to 0 ... 255 then back to 0

    Now how do you expect, say -128 to be compared with 255?
    Last edited by zacs7; 05-29-2008 at 11:06 PM.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks zacs7...... u solved the problem....

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Actually Salem did, I just answered the question you were supposed to

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Hai this is your output..

    Quote Originally Posted by Bargi View Post
    Hi,

    Can any body explain me the output of following:

    Code:
    #include<stdio.h>
    int array[]={1,2,3,4,5,6,7,8};
    #define SIZE (sizeof(array)/sizeof(int))
    
    int main()
    {
    printf("%d",SIZE);
    if(-1<=SIZE) printf("1");
    else printf("2");
    return 0;
    }


    SIZE is greater than -1 so it should print 2. It's working fine with positive numbers.

    Thanks
    Internally your -1 will be stored in 2's complement hence that will be some hex value
    hence that will be always greater then SIZE..condition fails and gets else part thats all

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I don't think it's about overflow. There are no operations on numbers except for the like operands of size_t and the relational operator. I think it has to do with integer promotions or something like that. But I'm not really good at that.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I never said it was about overflow.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, -1 is "the biggest value you can describe" when using it as a unsigned value. So NOTHING will be bigger than -1 in unsigned math.

    --
    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.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    zacs7, you were inferring that it was something to do with overflow in post 5, right?

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, I was mearly outlining the difference between unsigned and signed. ie, giving the user details as they asked. That you can't represent -k to k - 1 and 0 to 2k - 1 with n bits, it's one or the other.

    Although I can see how you thought I was talking about overflow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM
  3. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 06:58 PM
  4. edit controls in dialogs
    By Homunculus in forum Windows Programming
    Replies: 10
    Last Post: 02-23-2006, 03:38 PM
  5. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM