Mateusz_madi said:
Hi, can anybody explain why thist prog
#include<stdio.h>
int main()
{
int sx = -1;
unsigned ux = -1;
printf("%o\n", sx);
printf("%o\n", ux);
}
will print:
37777777777
37777777777
I mean how are values below zero evaluates in octal format?
Strictly speaking, your program's behavior is undefined. (That
doesn't mean it's going to blow up, just that the C standard doesn't
say what it will do.) The "%o" format requires an unsigned int
argument; in your first printf call, your passing it a (signed) int.
This is ok *if* the signed int value is within the numeric range
representable as an unsigned int, but in this case it isn't.
In practice, what will almost certainly happen is that the
*representation* of the signed int value will be interpreted as if it
were an unsigned int. On a two's-complement system, (int)-1 has the
same bit pattern as (unsigned int)UINT_MAX, which apparently is 2**32-1
on your system (4294967295 decimal, 037777777777 octal).
Note that the declaration
unsigned ux = -1;
doesn't store a negative value in ux (it can't). Instead, the int
value -1 is converted from int to unsigned int; by the language's
conversion rules, the result is guaranteed to be UINT_MAX.