473,387 Members | 1,650 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.

Comparing Char Against Integer Literal

What happens when you compare a char against an integer literal?
For example,

#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
signed char c1 = 150;
if (c1 < 150)
cout << "Less than 150.\n";
else
cout << "Not less than 150.\n";
if (static_cast<unsigned char>(c1) < 150)
cout << "Less than 150.\n";
else
cout << "Not less than 150\n";
return 0;
}

VC++6.0 outputs "Less than 150.\nNot less than 150\n". I understand why
this would happen, but my question is, is this right? This disturbs me
because sometimes I want to read bytes in from a file and I'd like to
think that I could compare them against a hex literal. What should I
do?

Zach

Jul 22 '05 #1
3 3435
Zach wrote in news:11*********************@f14g2000cwb.googlegro ups.com
in comp.lang.c++:
What happens when you compare a char against an integer literal?
For example,

#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
signed char c1 = 150;
if (c1 < 150)
cout << "Less than 150.\n";
else
cout << "Not less than 150.\n";
if (static_cast<unsigned char>(c1) < 150)
cout << "Less than 150.\n";
else
cout << "Not less than 150\n";
return 0;
}

VC++6.0 outputs "Less than 150.\nNot less than 150\n". I understand why
this would happen, but my question is, is this right?

Yep, the type of a literal like 150 is int so the compiler will convert
(widen) the signed or unsigned char to int before doing the comparison.

Note that the error (if you consider it such) is in assigning 150 to
a type (signed char) that can't represent the value.
This disturbs me
because sometimes I want to read bytes in from a file and I'd like to
think that I could compare them against a hex literal. What should I
do?


When ever you want to read "bytes" use unsigned char (or arrays or
std::vector<>s of) to store the "bytes".

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
"Zach" <za*********@comcast.net> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
What happens when you compare a char against an integer literal?
For example,

#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
signed char c1 = 150;


We can stop right here. On a machine with 8-bit chars, 150 does not fit in
a signed char.
The effect of assigning a value to a signed object that can't hold it is
undefined, so this program produces undefined behavior. Anything that might
happen after this point is irrelevant.
Jul 22 '05 #3

"Zach" <za*********@comcast.net> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
What happens when you compare a char against an integer literal?
For example,

#include <iostream>
#include <limits>
using namespace std;
int main(int argc, char* argv[]) {
signed char c1 = 150;
signed char c1(0);
int value(150);
signed char mx(std::numeric_limits<signed char>::max();

cout << "maximum possible value for type"
"'signed char' for this implementation is "
<< mx << '\n';

if(value <= mx)
c1 = value;
else
{
cout << "Value of " << value << " is outside the "
"allowed range for this implementation\n";
c1 = mx; /* or whatever 'default' you find appropriate */
}
if (c1 < 150)
cout << "Less than 150.\n";
else
cout << "Not less than 150.\n";
if (static_cast<unsigned char>(c1) < 150)
cout << "Less than 150.\n";
else
cout << "Not less than 150\n";
return 0;
}

VC++6.0 outputs "Less than 150.\nNot less than 150\n". I understand why
this would happen,
Are you sure? According to the standard, anything could happen
(your code's behavior is undefined).
but my question is, is this right?
Since the behavior is undefined, any result would be considered 'right'.
This disturbs me
because sometimes I want to read bytes in from a file and I'd like to
think that I could compare them against a hex literal. What should I
do?


If you want to read 'raw' bytes, always use type 'unsigned char'.
-Mike
Jul 22 '05 #4

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

Similar topics

3
by: yugandhar_S | last post by:
Hi, I have a quick question. The following works: char *s="hello"; *s="world"; but the following gives a segementation fault:
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
6
by: Pierre McCann | last post by:
Hello: I am writing the following program in C, but I do most of my coding in C++, and am not intimately familiar with the C notation for pointers. As such, I am trying to do a comparison...
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
9
by: subramanian100in | last post by:
Suppose we have char *a = "test message" ; Consider the comparison if (a == "string") ..... Here "string" is an array of characters. So shouldn't the compiler
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
0
by: steve.lorimer | last post by:
Thank you for taking the time to look at this: I'm looking for a pre-processor command that will allow me to resolve const strings into const char literals at compile time. Looking at the code...
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.