1. ## unsigned

when is the best time to use it.. and what does it really means? i have a gasp definition of it.. but would like to understand it better

2. as you might know , datatypes have a limited number range in memory wich can be found by the formula :

2^n - 1

where n is the size of a datatype in bits. An INT for example reserves 2 bytes (4 on some computers). An integer variable can hold any number from : -32,768 to 32,767 wich is the result of :

2^(2*8) - n

devided by 2 to cover both negative and positive numbers.

Now when you precede an INT with the keyword UNSIGNED , that INT will only have positive values ranging from : 0 to 65,535. (didn't devide it by 2 because its only for positive numbers).

Basically , you use UNSIGNED you need to make a variable big enough to hold your large positive values.

*EDIT* to figure out how big is a datatype , simply use the sizeof() function. For example :
Code:
`printf("%d", sizeof(int));`
or
Code:
`printf("%d", sizeof(unsigned int));`
hope this helped.

3. i understood it.. yet i didnt understand it.. but it still help me out.. cause now i do understand a bit more then before. From what i understand that i didnt know before is that unsigned will always give you a positive number and it should be used when you want to pass large possitive numbers to an array.. if im correct

p.s

thank you

4. Originally Posted by cigcode
i understood it.. yet i didnt understand it.. but it still help me out.. cause now i do understand a bit more then before. From what i understand that i didnt know before is that unsigned will always give you a positive number and it should be used when you want to pass large possitive numbers to an array.. if im correct

p.s

thank you
Please don't use the title as an integral part of your question..

You have a basic grasp of the situation, but where the array part came in is beyond me.

Signed: positive and negative numbers
Unsigned: positive numbers only, twice as many as signed.

That's basically it.

5. oh hmm sorry i didnt know i did something wrong.. if i did then sorry about that.. some people in this world take things differently.. like you just did.. thank you anyways

6. if thats true, can you tell me why this works:
Code:
```unsigned int test = -1;

printf("%d",test);```

7. hm i think cause int takes 4 bits of space and all you are doing is removing one as well as unsigned takes 4 bit of space + your value is a low value.. and unsigned works on large ones

but then again i could be wrong.. cause im still learning and still new to c.. so please if im wrong i would like to be corrected

8. The reason
Code:
```unsigned int test = -1;
printf("%d",test);```
works is because of this: Everything is reprensented as a bit pattern, only thing that changes is how we interpet that bit pattern and that determines what it is.

Because bit patterns have no particular way to represent negitive number, by convention we have determined a way to interpet them so that we can represent negitive numbers.

The bit pattern for UINT_MAX (for a 32 bit system) is:
11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
and the bit pattern for -1 is ( for a 32 bit system)
11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111

The difference? Just how we interpet the bit pattern.

when you use "%d" in printf we tell the function to interpet the bit pattern as signed so we get -1, if we had used "%u" we would have gotten 4294967295.

9. >Signed: positive and negative numbers
>Unsigned: positive numbers only, twice as many as signed.
>
>That's basically it.
Ah, but you forget overflow guarantees. Signed overflow is guaranteed to be undefined, whereas unsigned overflow is guaranteed to have a result that remains within the bounds of the type size by calculating modulo 2^n and rolling over. So
Code:
`unsigned int test = -1;`
Is equivalent to UINT_MAX because of overflow toward the negative.

>if thats true, can you tell me why this works
You got lucky? If the type of the value and the format flag to printf designating the type do not match, the behavior is undefined. That includes unsigned qualifiers for otherwise equivalent types.

>unsigned will always give you a positive number and it should be used when you want to pass large possitive numbers to an array
No, not really. It's possible to legally use negative indices into an array because subtraction is legal in pointer arithmetic. As long as you dereference memory that you own, all is well:
Code:
```#include <stdio.h>

int main ( void )
{
char *s = "test";

s++;
printf ( "%c\n", s[-1] );

return 0;
}```
However, for the most part you're correct, and the standard even gives us a type definition that is perfect for array indices, size_t.

>An INT for example reserves 2 bytes (4 on some computers).
Or more. The size of an int must be at least 16 bits, but there is no restriction on it being larger.

>An integer variable can hold any number from : -32,768 to 32,767
Not for all bit representations. To be absolutely correct and portable, a 16 bit integer can hold any value from -32,767 to 32,767.

10. Originally Posted by Thantos
The bit pattern for UINT_MAX (for a 32 bit system) is:
11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
and the bit pattern for -1 is ( for a 32 bit system)
11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
So in a "UINT_MAX (for a 32 bit system)," how do you cram those 64 bits into 32?

Originally Posted by Prelude
>Signed: positive and negative numbers
>Unsigned: positive numbers only, twice as many as signed.
>
>That's basically it.
Ah, but you forget overflow guarantees.
Well, since I was answering a noob, I decided not to give the entire disertation on the concept of numerical values at this point.

My motto:
"Let them get comfortable with the simple. We can confuse them with complex details later. This way their heads won't explode"

11. how do you cram those 64 bits into 32?
That is a special property of law that occurs at 11:12 PM when one is really tired.

12. Originally Posted by Thantos
That is a special property of law that occurs at 11:12 PM when one is really tired.
Ahh, yes. We must be in a different time zone. That occurrs for me around 4:30AM