top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the need of signed char in c? When we will use the signed char in c?

+2 votes
499 views
What is the need of signed char in c? When we will use the signed char in c?
posted Nov 3, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Use only signed char and unsigned char types for the storage and use of numeric values because it is the only portable way to guarantee the signedness of the character types.

So lets play with the example and try to understand:

int main()
{
    char c = 200;   //by default it a signed char type.
    int i = 1000;
    printf("i/c = %d\n", i/c);
    return 0;
}

Here the output will be -17 not 5 because of signed data, as we know a char can store a data of size 1byte ie. 8bits.
Basically its range to store a number is form "-128 to 127" so here we want to assing a number 200 which is more than its range. So it will store -56 instead of 200.
Its simple, a char range is -128 to 127. so if you want to store a numbe which is greater than 127. it will store the number next fit .( -128 , -127 ......-1, 0, 1, ---- 126, 127, -128 ....so on)

Change the char to unsigned char in the above program and see the result, the output will be 5.
for unsigned char the range is (0 to 255).

answer Nov 3, 2014 by Arshad Khan
Nice explanation :)
Thanks for appreciation :)
Similar Questions
+2 votes

A char can not be a +ve or -ve. last bit is signed bit in accumulator.but not useful for CHAR Whats the purpose? of doing it.

+1 vote
#include <stdio.h>
int main()
{
  char val=250;
  int ans;
  ans= val+ !val + ~val + ++val;
  printf("%d",ans);
  return 0;
}
...