473,499 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned char and -1

I came across the following piece of code:

#define ERROR -1
#define STATUS0 0
#define STATUS1 1
#define STATUS2 2

unsigned char foo()
{
if (/* condition-A */) return ERROR; /* is that safe? */
if (/* condition-B */) return STATUS0;
if (/* condition-C */) return STATUS1;
return STATUS2;
}

Is '-1 returned as unsigned char' safe in the given context?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Oct 25 '05 #1
7 2606
Alex Vinokur wrote:
I came across the following piece of code:

#define ERROR -1
#define STATUS0 0
#define STATUS1 1
#define STATUS2 2

unsigned char foo()
{
if (/* condition-A */) return ERROR; /* is that safe? */
if (/* condition-B */) return STATUS0;
if (/* condition-C */) return STATUS1;
return STATUS2;
}

Is '-1 returned as unsigned char' safe in the given context?


In the given (restricted) context, yes, it returns UCHAR_MAX.
[Any value not in the range of an unsigned type is converted
modulo 1+UTYPE_MAX.]

However, in broader contexts, it's not so safe, e.g. ...

if (foo() == ERROR)

This condition will likely fail even though foo returns ERROR.
This is because the UCHAR_MAX will likely be promoted to a
int, and -1 is not UCHAR_MAX.

--
Peter

Oct 25 '05 #2

"Peter Nilsson" <ai***@acay.com.au> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
Alex Vinokur wrote:
I came across the following piece of code:

#define ERROR -1
#define STATUS0 0
#define STATUS1 1
#define STATUS2 2

unsigned char foo()
{
if (/* condition-A */) return ERROR; /* is that safe? */
if (/* condition-B */) return STATUS0;
if (/* condition-C */) return STATUS1;
return STATUS2;
}

Is '-1 returned as unsigned char' safe in the given context?
In the given (restricted) context, yes, it returns UCHAR_MAX.
[Any value not in the range of an unsigned type is converted
modulo 1+UTYPE_MAX.]

However, in broader contexts, it's not so safe, e.g. ...

if (foo() == ERROR)


And in this context?

unsigned char ch1, ch2;
ch1 = foo();
/* Stuff-1 */
if (ch1 == ch2)
{
/* Stuff-2 */
}

This condition will likely fail even though foo returns ERROR.
This is because the UCHAR_MAX will likely be promoted to a
int, and -1 is not UCHAR_MAX.

[snip]
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Oct 25 '05 #3

Alex Vinokur wrote:
And in this context?

unsigned char ch1, ch2;
ch1 = foo();
/* Stuff-1 */
if (ch1 == ch2)
{
/* Stuff-2 */
}


and in this context ch2 is undefined :-p

Oct 25 '05 #4

<ma************@gmail.com> wrote in message news:11*********************@g47g2000cwa.googlegro ups.com...

Alex Vinokur wrote:
And in this context?

unsigned char ch1, ch2;
ch1 = foo();
/* Stuff-1 */
if (ch1 == ch2)
{
/* Stuff-2 */
}


and in this context ch2 is undefined :-p


Stuff-1 can change ch2.
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Oct 25 '05 #5
Alex Vinokur wrote:
I came across the following piece of code:

#define ERROR -1
#define STATUS0 0
#define STATUS1 1
#define STATUS2 2

unsigned char foo()
{
if (/* condition-A */) return ERROR; /* is that safe? */ [...] Is '-1 returned as unsigned char' safe in the given context?


[comp.lang.c answer]
Yes, it is safe in the sense that UCHAR_MAX will be returned.
No, it is not safe, because 'ERROR' invades the namespace reserved to
the implementation.

As a general rule, crossposting to comp.lang.c and comp.lang.c++ is a
poor idea. Not only are these different languages, but when the syntax
is the same in each language, the semantics may differ, and when the
syntax and semantics agree, the accepted best practice may differ.
Oct 25 '05 #6

Alex Vinokur wrote:
unsigned char ch1, ch2;
ch1 = foo();
/* Stuff-1 */
if (ch1 == ch2)
{
/* Stuff-2 */
}


and in this context ch2 is undefined :-p


Stuff-1 can change ch2.


A comment can change variable? Now that's something new :-p''

Oct 25 '05 #7
Martin Ambuhl wrote:
Alex Vinokur wrote:
I came across the following piece of code:

#define ERROR -1
#define STATUS0 0
#define STATUS1 1
#define STATUS2 2

unsigned char foo()
{
if (/* condition-A */) return ERROR; /* is that safe? */

[...]
Is '-1 returned as unsigned char' safe in the given context?


[comp.lang.c answer]
Yes, it is safe in the sense that UCHAR_MAX will be returned.
No, it is not safe, because 'ERROR' invades the namespace reserved
to the implementation.


True, although EXXXX identifiers are only reserved if the code
includes <errno.h>. That said, it is best to avoid defining such
identifiers at all, in case the code ever gets included by other
code that might use <errno.h>.

--
Peter

Oct 26 '05 #8

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

Similar topics

19
6445
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
31483
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
15607
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
1042
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
41890
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
7760
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
2830
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
11608
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
8
2317
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
9922
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
7128
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
7006
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
7169
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
7385
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
5467
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,...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.