Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C

Name: Anonymous 2011-03-12 1:32

Hi /prog/

I just notice this strange thing in C:

int a[4];
#define n (sizeof(a) / sizeof(a[0]))
int d = n;

if (-1 < n) {
    printf("yup\n");
}

if (-1 < d) {
    printf("cuack\n");
}

Why is that it doesn't print "yup"?

Name: Anonymous 2011-03-12 8:13

sizeof(int) is unsigned.

if (-1 < (int)sizeof(int)) {
    printf("yup\n");
}

works.

I'm guessing but:

-1 in twos complement would be 1 << sizeof(int) (If I remember correctly).

sizeof(int) would be 4 (usually) unsigned or otherwise.

Could explain why -1 > sizeof(int).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List