473,387 Members | 1,621 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,387 software developers and data experts.

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 2604
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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...

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.