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 ???. 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
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
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.
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.
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.
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.
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.
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.
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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: =?gb2312?B?wNbA1rTzzOzKpg==?= |
last post by:
i wrote:
-----------------------------------------------------------------------
----------------------------------------
unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
|
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...
|
by: Kenzogio |
last post by:
Hi,
I have a struct "allmsg" and him member :
unsigned char card_number; //16
allmsg.card_number
|
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...
|
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |