473,395 Members | 1,641 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.

unsigned char

Does the c standard state that for all c99 implementation char as standard
is equal unsigned char ??? - or do i have to take this into account when
writing my program ???.
Nov 14 '05 #1
10 14374
Lars Tackmann wrote:
Does the c standard state that for all c99 implementation char as standard
is equal unsigned char ?


Not at all. It's equivalent to either signed char or unsigned char.
The choice is up to the implementation.

It's not _equal_ to either, though. It's a distinct type, so
char *foo;
unsigned char *bar = foo;
is an error even if char is equivalent to unsigned char.

--
Hallvard
Nov 14 '05 #2
Lars Tackmann <ro****@diku.dk> wrote:
Does the c standard state that for all c99 implementation char as standard
is equal unsigned char ??? - or do i have to take this into account when
writing my program ???.


The implementation has to pick one from {signed|unsigned} char
for "plain" char:

ISO/IEC 9899:1999 6.2.5#15
The three types char, signed char, and unsigned char are
collectively called the character types. The implementation
shall define char to have the same range, representation,
and behavior as either signed char or unsigned char.

and

ISO/IEC 9899:1999 6.3.1.1#3
The integer promotions preserve value including sign. As
discussed earlier, whether a "plain" char is treated as
signed is implementation-defined.
HTH
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.angelfire.com/ms3/bchambl...me_to_clc.html
clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...acllc-c++.html
Nov 14 '05 #3
Hallvard B Furuseth wrote:
Lars Tackmann wrote:

Does the c standard state that for all c99 implementation char as standard
is equal unsigned char ?

Not at all. It's equivalent to either signed char or unsigned char.
The choice is up to the implementation.

It's not _equal_ to either, though. It's a distinct type, so
char *foo;
unsigned char *bar = foo;
is an error even if char is equivalent to unsigned char.


Note that pointers to unsigned char can point to any byte in an object.
So if foo had been properly initialised the above would have been legal.

But even
int *p;
int *p2 = p;

is an error since it uses the value of an uninitialised variable.

--
Thomas.

Nov 14 '05 #4
Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:
int *p;
int *p2 = p;


Is ( p==NULL ) likewise undefined?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
Christopher Benson-Manica wrote:
Thomas Stegen <ts*****@cis.strath.ac.uk> spoke thus:
int *p;
int *p2 = p;


Is ( p==NULL ) likewise undefined?


Yes. No expression that uses the value of an uninitialized pointer
variable has defined behaviour.

Jeremy.
Nov 14 '05 #6
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> spoke thus:
Yes. No expression that uses the value of an uninitialized pointer
variable has defined behaviour.


Does that hold true for non-pointer variables as well? Such as

int i;
if( i != 42 ) {
printf( "What a tragedy...\n" );
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7
Christopher Benson-Manica wrote:
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> spoke thus:
Yes. No expression that uses the value of an uninitialized pointer
variable has defined behaviour.
Does that hold true for non-pointer variables as well?


Not in every case. Since the bit pattern of an uninitialized object
is indeterminate, whether the behaviour is defined depends on whether
all

It does, but the particular type of behaviour - undefined,
unspecified, etc. - depends on the type of the variable and possibly
on the implementation. For example, all bit patterns have valid
unsigned char values, so using an uninitialized unsigned char object
in an arithmetic expression never invokes undefined behaviour. Some
such expressions may even have well-defined behaviour; the following
assertion cannot fail.

unsigned char c;
assert(!(c - c));
Such as

int i;
if( i != 42 ) {
printf( "What a tragedy...\n" );
}


It's unspecified whether or not this code has undefined behaviour. On
an implementation where all possible bit patterns represent valid int
values the behaviour is not undefined, because the standard imposes
requirements on it: `i' has either the value 42 or some other valid
value. On an implementation with trap values for int, the code has
undefined behaviour.

Jeremy.
Nov 14 '05 #8
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> spoke thus:
Not in every case. Since the bit pattern of an uninitialized object
is indeterminate, whether the behaviour is defined depends on whether
all
Lose your train of thought? Did Agent Smith pay you an unwelcome
visit? ;)
It does, but the particular type of behaviour
(etc.)


Glad to see Agent Smith let you live this time. Seriously, thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9
Thomas Stegen wrote:
Hallvard B Furuseth wrote:
char *foo;
unsigned char *bar = foo;
is an error even if char is equivalent to unsigned char.
Note that pointers to unsigned char can point to any byte in an object.
So if foo had been properly initialised the above would have been legal.


No, assignment between different pointer types are not legal.
OTOH, using a cast is OK: unsigned char *bar = (unsigned char*) foo;
But even
int *p;
int *p2 = p;

is an error since it uses the value of an uninitialised variable.


Oops. I should have said 'char *foo = "something";'.

--
Hallvard
Nov 14 '05 #10
"Thomas Stegen" <ts*****@cis.strath.ac.uk> wrote:
Hallvard B Furuseth wrote:
It's not _equal_ to either, though. It's a distinct type, so
char *foo;
unsigned char *bar = foo;
is an error even if char is equivalent to unsigned char.
Note that pointers to unsigned char can point to any byte in
an object. So if foo had been properly initialised the above
would have been legal.


Well, no, it would still be a constraint violation because of the
incompatible types in the initialisation. The type (char *) is
never compatible with the type (unsigned char *), and nor is it
ever compatible with the type (signed char *).

To make it legal you can either use a cast, or an intermediate
variable of type (void *) as conversions to and from (void *)
do not require casts.

Example with cast:
char *foo = 0; /* Properly initialise */
unsigned char *bar = (unsigned char *)foo; /* Convert with cast */

Example without cast:
char *foo = 0; /* Properly initialise */
void *bar = foo; /* Convert to (void *) */
unsigned char *baz = bar; /* Convert from (void *) */
But even
int *p;
int *p2 = p;

is an error since it uses the value of an uninitialised variable.


Though this is not a contraint violation, it is merely undefined
behaviour, so the compiler is not required to detect it and
generate a diagnostic message.

--
Simon.
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

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 ...
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...
4
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
3
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
5
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
8
by: Steven | last post by:
Hello, everyone! I find a version of strcpy(), I don't know why it return the unsigned char value. Can I change it into return *s1-*s2? int strcmp(const char *s1, const char *s2) { while...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.