I'm sorry if this question is too basic...I just have not found the answer to it anywhere.
Say I declare a C variable like this:
unsigned int var = 241;
In this case the var is unsigned so my intention is for it to have decimal value 241.
Alternatively I could declare it like this:
signed int var = -15;
In this case I declared it as signed integer so, as per my understanding it should have the decimal value -15.
However both times, I assume the var will be declared in memory(hardware) like this: 1111 0001.
So how does the processor know, at the lowest level which is in the hardware that I intended to declare this as 241 or -15? I'm aware of the two's complement notation that is used to represent negative numbers and such but, I assume in hardware the processor only sees a sequence of ones and zeroes and then does some operation with it by switching the states of some ICs. How does the processor know whether to interpret the sequence of bits in standard binary(for unsigned) or 2's complement(for signed)?
Also another somewhat unrelated questions:
In C I can do this:
unsigned int var = -15; printf("The var is: %d ", var); This will as expected print -15. Why, when I do this:
signed int var = 0xF1; //or 0b11110001 printf("The var is: %d ", var);
I get 241 instead of -15? Since I declared it as signed and in two's complement 0xF1 is -15 why am I getting the value 241 which is the equivalent of 0xF1 in standard binary?
- Why does the compiler let me do stuff like: unsigned int var = -15;
Shouldn't it throw an error telling me I can't assign negative values to a variable which I have declared as unsigned?
Thank you and I apologize for my many and perhaps basic questions, there is so much I do not know :D.