#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;
}

how is SIZE casted in this example? Because the compiler believes -1 > SIZE (which is 8). Is this because it was treated as unsigned char and -1 looped around to become 255?

Recommended Answers

All 2 Replies

Yes, sizeof() evaluates to an unsigned number.

#define is a compiler directive and it is used to make substitutions throughout the file in which it is located.
So, a symbol SIZE is replace with (sizeof(array)/sizeof(int))
>how is SIZE casted in this example?
When you compare an unsigned int and a signed int in this
way,the signed int will be converted to unsigned int. Converting
a negative signed int to a unsigned int is done by adding 1 to max_range_value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.