Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I understood processor don't know whether the number is signed or not while executing the instruction. While printing the number alone the sign get into picture. Say if i use %d then the number will be interpreted as signed and output will be printed based on the 2's complement representation of the number. Above is my understanding , please correct me if I am wrong.

My doubt is, what is the difference between signed char and unsigned char(let' take char for simplicity since 8 bit) if I am going to use in my program.

for example.

C
signed char ch = 200; 


If I use this 'ch' as an array index as below.
C
int arr[210]=  {0}; //initialize all 210 to zero
arr[ch]='H';  //Here ch will be 200  or -56 ???

printf("val = %c\r\n", arr[ch])  //output printed is 'H'; 200th position is set to 'H'

based on the output i see compiler is not taking arr[-56], it is taking arr[200].

since it is a signed char range is from -128 to +127. so 200 will be interpreted as -56(while printing via %d). But in memory any way the value will be 11001000.
so if my program is not going to print the value then there is no differnce in using signed vs unsigned char . Is this understanding right? Or Am i confusing something here?Please clarify.
Posted
Updated 23-Nov-15 2:54am
v3
Comments
Kornfeld Eliyahu Peter 23-Nov-15 8:22am    
You are going along a wrong, line...Every value stored in memory is subject to interpretation and there is no meaning of the value without it.
It is true that the binary value 00101011 can be interpreted in different way and still the memory block does not change, but it is meaningless, because all the code you write IS working because you interpret those memory blocks...
chandrAN2& 23-Nov-15 8:25am    
sorry i didn't got it. could you please comment on the example which i mentioned above?

Quote:
based on the output i see compiler is not taking arr[-56], it is taking arr[200].
Sorry, that is the wrong conclusion.

To get the correct one, see the output of
printf("val = %c\r\n", arr[200]); 
 
Share this answer
 
The value of ch will be -56. You can check that by printing it:
C++
printf("ch = %d\n", ch);

If you want to be really sure you can print the address:
C++
printf("addr of arr: %p, addr of arr[h]: %p\n", arr, &arr[ch]);

The output might look like this:
addr of arr: 0xbe8db170, addr of arr[h]: 0xbe8db090


As a result, the array is accessed out of bounds. When enabling all compiler warnings, you should get an appropriate warning.

Your assumption that arr[200] is 'H' in this line is wrong:
C++
printf("val = %c\r\n", arr[ch])  //output printed is 'H'; 200th position is set to 'H'

This can be verified by passing -56 and 200 instead of ch.
 
Share this answer
 
Comments
chandrAN2& 24-Nov-15 7:16am    
https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html .
The above link nicely explain why the 2's complement is working for negative numbers.
Thanks for your clear explanation
The processor doesn't know if the value is signed or not. That concept is built entirely into your code. It's how YOU treat the value that determines if the value is going to be signed or not. The only thing is is that YOU have to treat it as a certain value type consistently in your code.
 
Share this answer
 
Comments
chandrAN2& 24-Nov-15 7:14am    
Thanks Dave

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900