Given
signed char str_a[]="Hello, world!\n";
unsigned char str_b[]="Hello, world!\n";
what is the difference, if any, between the following two statements?
printf( "%s", str_a );
printf( "%s", str_b );
If there is a difference, what is the best way to compare *str_a with
0xFF? (On my implementation, unadorned char is signed, and so I'm
using
if( *str_a == (signed char)0xFF ) ...
to quiet compiler warnings.)
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome. 19 4679
Christopher Benson-Manica wrote: Given
signed char str_a[]="Hello, world!\n"; unsigned char str_b[]="Hello, world!\n";
what is the difference, if any, between the following two statements?
printf( "%s", str_a ); printf( "%s", str_b );
If there is a difference, what is the best way to compare *str_a with 0xFF?
char type arguments are converted to int.
That result is converted to unsigned char for stdio output.
There shouldn't be a problem there, regardless of sign of char.
(On my implementation, unadorned char is signed, and so I'm using
if( *str_a == (signed char)0xFF ) ...
if( *(unsigned char*)str_a == -1 )
/* assuming 0xff is meant to be all bits set */
--
pete
On Thu, 12 Feb 2004 17:52:55 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote: Given
signed char str_a[]="Hello, world!\n"; unsigned char str_b[]="Hello, world!\n";
what is the difference, if any, between the following two statements?
printf( "%s", str_a ); printf( "%s", str_b );
Other than that they take different arguments? None that I can see.
There are no conversions to worry about, and once the pointer values
land in printf, there's no way for printf to be able tell the
difference anyway (and no reason for it to care). If there is a difference, what is the best way to compare *str_a with 0xFF? (On my implementation, unadorned char is signed, and so I'm using
if( *str_a == (signed char)0xFF ) ...
to quiet compiler warnings.)
I'm not sure how the 2nd question is dependent upon the first... What
you've done is clearly showing your intent, which is a Good Thing. One
alternative is
if (*str_a == -1) ...
but I like yours better.
Leor Zolman
BD Software le**@bdsoft.com www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:c0******** **@chessie.cirr .com... Given
signed char str_a[]="Hello, world!\n"; unsigned char str_b[]="Hello, world!\n";
what is the difference, if any, between the following two statements?
printf( "%s", str_a ); printf( "%s", str_b );
'^' != (unsigned char)'^' for example.
Extended ascii chars have negative values (since they are higher than 127).
pete <pf*****@mindsp ring.com> spoke thus: if( *(unsigned char*)str_a == -1 )
1) Is that better than ... (unsigned char)*str_a ... ?
2) This relies on -1's representation being all bits set, yes?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
pete wrote: Christopher Benson-Manica wrote: Given
signed char str_a[]="Hello, world!\n"; unsigned char str_b[]="Hello, world!\n";
what is the difference, if any, between the following two statements?
printf( "%s", str_a ); printf( "%s", str_b );
If there is a difference, what is the best way to compare *str_a with 0xFF? char type arguments are converted to int.
True (usually), but irrelevant: the arguments are not
any kind of `char', but are pointers.
That result is converted to unsigned char for stdio output.
Either the Standard doesn't say so, or I've overlooked
the spot where it does.
There shouldn't be a problem there, regardless of sign of char.
There isn't a problem, because the "%s" specifier is defined
to work with any of `char*', `unsigned char*', and `signed char*'.
Something of an oddity, really: Most conversion specifiers are
very strict about the type of the corresponding argument, yet
here's one that accepts arguments of three distinct types. (On my implementation, unadorned char is signed, and so I'm using
if( *str_a == (signed char)0xFF ) ...
Undefined behavior, I think. You might do better with
if ( (unsigned char)*str == 0xFF )
-- Er*********@sun .com
Christopher Benson-Manica wrote: pete <pf*****@mindsp ring.com> spoke thus:
if( *(unsigned char*)str_a == -1 ) 1) Is that better than ... (unsigned char)*str_a ... ?
No. Now, I like
if( (unsigned char)*str_a == (unsigned char)-1)
or
if( (unsigned char)*str_a == (unsigned char)0xFF)
best.
2) This relies on -1's representation being all bits set, yes?
If str_a points to an all bits set byte,
then *(unsigned char*)str_a will equal ((unsigned char)-1)
then the question becomes, is
((unsigned char)-1) equal to (-1) ?
and it isn't, so I was wrong.
if( (unsigned char)*str_a == (unsigned char)0xFF )
The conversion of out of range values to signed char,
is implementation defined, so I would avoid it.
The conversion of everything to unsigned char, is well defined.
0xff is of type int.
--
pete
Eric Sosman wrote: pete wrote: That result is converted to unsigned char for stdio output.
Either the Standard doesn't say so, or I've overlooked the spot where it does.
I think pete's right. "Byte output" functions, printf included, are
defined in terms of fputc:
The byte output functions write characters to the stream as if by
successive calls to the fputc function. [7.19.3#12]
and the description of fputc says:
The fputc function writes the character specified by c (converted
to an unsigned char) to the output stream pointed to by stream
[...] [7.19.7.3#2]
Jeremy.
On Thu, 12 Feb 2004 18:16:12 GMT, Leor Zolman <le**@bdsoft.co m> wrote: if( *str_a == (signed char)0xFF ) ...
oops, of course that's UB... Eric caught it.
Leor Zolman
BD Software le**@bdsoft.com www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
"Martin Johansen" <ma******@is.on line.no> writes: "Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message news:c0******** **@chessie.cirr .com... Given
signed char str_a[]="Hello, world!\n"; unsigned char str_b[]="Hello, world!\n";
what is the difference, if any, between the following two statements?
printf( "%s", str_a ); printf( "%s", str_b );
'^' != (unsigned char)'^' for example.
Extended ascii chars have negative values (since they are higher than 127).
<Slightly OT>
Actually the ASCII value of '^' is 94 (unless my newsreader mangled
whatever extended ASCII character you actually wrote).
</Slightly OT>
All the characters in the string literals above are in the "basic
execution character set". C99 6.2.5p3 says:
If a member of the basic execution character set is stored in a
char object, its value is guaranteed to be positive.
In ASCII, all such characters happen to have values in the range
32..126. In EBCDIC, if I recall correctly, some basic characters have
codes greater than 127; I think this implies that in an implementation
that uses EBCDIC as its execution character set, type char must be
unsigned (assuming CHAR_BIT==8).
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be" 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 answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
|
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
template <>
struct to_unsigned<signed int> : public std::unary_function<signed int,
unsigned int>
{
unsigned int operator()(signed int x) const { return x; }
};
|
by: dam_fool_2003 |
last post by:
For int data type the default range starts from signed to unsigned. If
we don't want negative value we can force an unsigned value. The same
goes for long also.
But I don't understand why we have signed char which is -256. Does it
means that we can assign the same ASCII value to both signed and
unsigned. That means the ASCII value can be represented with a type of
signed char and also unsigned char?
For example
int main(void)
{
|
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 standard doesn't even care what a normal char
is (because signed and unsigned have equal behavior).
For example if someone does this:
unsigned char a = -2; /* or = 254 */
|
by: juanitofoo |
last post by:
Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:
#include <stdio.h>
int main()
{
signed char *p = "Hola";
| |
by: Marcin Kalicinski |
last post by:
Are 3 types: signed char, char and unsigned char distinct? My compiler is
treating char as signed char (i.e. it has sign, and range from -128 to 127),
but the following code does not call f<char> as I would expect:
template<class T> void f(T t)
{
}
template<> void f<char>(char t)
{
|
by: Frederick Gotham |
last post by:
I'd like to discuss the use of signed integers types where unsigned integers
types would suffice.
A common example would be:
#include <cassert>
#include <cstddef>
int CountOccurrences(unsigned char const val,
unsigned char const p,
|
by: somenath |
last post by:
Hi All,
I am trying to undestand "Type Conversions" from K&R book.I am not
able to understand the
bellow mentioned text
"Conversion rules are more complicated when unsigned operands are
involved. The problem
is that comparisons between signed and unsigned values are machine-
dependent, because
they depend on the sizes of the various integer types. For example,
suppose that int is 16 bits
|
by: Kislay |
last post by:
Consider the following code snippet
unsigned int i=10;
int j= - 2; // minus 2
if(i>j)
cout<<"i is greater";
else
cout<<"j is greater";
Since i is unsigned , j is greater . I know why , but vaguely . Can
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |