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 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
"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
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
<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
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.
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''
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 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: 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: 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,...
|
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,...
|
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...
|
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...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |