473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Diff between signed char and char.

I would think they are the same. However, when I try both in functions like strcpy() I can only use char. Otherwise the warning is "pointer targets in passing arg 1 of 'strcpy' differ in signedness, when trying with signed char. Please help I don't get it - aren't they the same?
Nov 24 '08 #1
4 1688
vmpstr
63
The default signedness of char is not in any standard, AFAIK. I believe that's compiler dependent.

The general rule is that if you care about the signedness, then declare it explicitly. That is, if you will be assigning values to it above 127, use unsigned char. If you must have negative values, use signed char.

For dealing with regular, english language strings, you can safely leave out the signed/unsigned qualifier.

Edit:

But to answer your question in a straight forward fashion,

char is either unsigned char or signed char. It's not any other type. Whether it is signed or unsigned is decided by the compiler
Nov 24 '08 #2
donbock
2,426 Expert 2GB
@dissectcode
There are three flavors of the 'char' type. The third flavor shown below is referred to as 'plain char'.
Expand|Select|Wrap|Line Numbers
  1. signed char v1;
  2. unsigned char v2;
  3. char v3;
Plain char is used to hold character constants (such as 'A'). Whether plain char is a signed or unsigned type is implementation dependent (that is, it can vary from compiler to compiler). However, if you only use plain char to hold character constants then you don't care if it is signed or unsigned.

I recommend a simple rule of thumb: use 'signed char' or 'unsigned char' to hold numbers, and use plain char to hold character constants. You'll find this expressed more eloquently in MISRA-C v2.

Your difficulty with strcpy comes from the function prototype, not from any inherent limitation on the various flavors of char. The function prototype for strcpy specifies pointer-to-plain-char so calling it with any other argument type provokes compiler errors.
Nov 25 '08 #3
@dissectcode
as said above they depend on the compiler,
but if char is used the first bit is set 1 or 0 . it will some times be a problen when its first address is refferd to some where, eg
print it character wise , signed or char will print as
FFFFFF8c (read in hes because unreadable format)
else for unsigned char
it will show the proper 8c only (in Hex)
signed char wont be any proplem when you are not doing any functions such as memcpy , or realloc with this char*. fi you do compiler should be compatible else safe to use unsigned char
and try in hex for proper checking
run this eg
you will understand the first bit set as 1 is a great problem
*************
#include <stdio.h>
#include <stdlib.h>

int main()
{
unsigned char *unsigbuff;
char *sigbuff;
unsigbuff="76031\r\nŒ„˜RMfg3QouGQQAAAXFAAAALwAAHSU AAAAE";
sigbuff="76031\r\nŒ„˜RMfg3QouGQQAAAXFAAAALwAAHSUAA AAE";
int i=0;
while(i < 40)
{
if(unsigbuff[i] == '\r' )
{
i++;
if(unsigbuff[i] == '\n')
{
i++;
printf("_u: %2X,%2X",unsigbuff[i],sigbuff[i]);
}

}
else{
i++;}
}
return 0;
}
Nov 26 '08 #4
donbock
2,426 Expert 2GB
If the purpose of the code provided by mithuncm is to determine whether plain char is signed or unsigned, then a much simpler way is to interrogate <limits.h>. According to the C89 standard, that header provides the following information:
SCHAR_MIN is the minimum value for an object of type 'signed char' (-127).
SCHAR_MAX is the maximum value for an object of type 'signed char' (+127).
UCHAR_MAX is the maximum value for an object of type 'unsigned char' (+255).
CHAR_MIN is the minimum value for an object of type 'char' (see note).
CHAR_MAX is the maximum value for an object of type 'char' (see note).

If the value of an object of type 'char' is treated as a signed integer when used in an expression, the value of CHAR_MIN shall be the same as that of SCHAR_MIN and the value of CHAR_MAX shall be the same as that of SCHAR_MAX. Otherwise, the value of CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same as that of UCHAR_MAX.
That is,
Expand|Select|Wrap|Line Numbers
  1. #include <limits.h>
  2. #include <stdio.h>
  3.  
  4. /* Run-time determination */
  5. if (CHAR_MIN != 0)
  6.     printf("Plain char is signed\n");
  7. else
  8.     printf("Plain char is unsigned\n");
  9.  
  10. /* Compile-time determination */
  11. #if CHAR_MIN
  12.     printf("Plain char is signed\n");
  13. #else
  14.     printf("Plain char is unsigned\n");
  15. #endif
Nov 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
64
by: ng5000 | last post by:
Hi, What's the point of a signed char? As I see it a char represents a character (not an integer, use an int type e.g. short int if you want an 8 bit number, or one of the new types, uint8 I...
22
by: juanitofoo | last post by:
Hello, I've just switched to gcc 4 and I came across a bunch of warnings that I can't fix. Example: #include <stdio.h> int main() { signed char *p = "Hola";
8
by: Marcin Kalicinski | last post by:
Are 3 types: signed char, char and unsigned char distinct? My compiler is treating char as signed char (i.e. it has sign, and range from -128 to 127), but the following code does not call f<char>...
21
by: Comp.Lang.c | last post by:
Hello Iam Niranjan... i have one small doubt. void main() { int size; size = sizeof(int *); printf("\n size of int *------>%x"size); size = sizeof(char *); printf("\n size of char...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.