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

why " for(char i=64; i<128; i++ ) " gives an infinite loop?

why the following program using "char" gives me an infinite loop? If I
change char to int, it works fine.

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
for(char i=64; i<128; i++ )
cout<<i<<endl;

return 0;
}

Apr 8 '07 #1
9 3475
In GCC, a warning tells you exactly what you need to know:

"warning: comparison is always true due to limited range of data type"

Apr 8 '07 #2
#include <climits>

int main()
{
std::cout << CHAR_MIN << " and " << CHAR_MAX;
}

On my system, and on most systems, this would yield:

-128 and 127

(I accidentally posted my first message before I was done).

Apr 8 '07 #3
ha********@gmail.com wrote:
why the following program using "char" gives me an infinite loop? If I
change char to int, it works fine.

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
for(char i=64; i<128; i++ )
cout<<i<<endl;

return 0;
}
A char has a maximum value of 127, thus the comparison (i<128) is always
true.
Apr 8 '07 #4
Thanks Einar Tønnessen and Siddhartha Gandhi:

you guys solved my problem. It is the limit thing.

Apr 9 '07 #5
On Apr 8, 8:01 pm, haijin....@gmail.com wrote:
Thanks Einar Tønnessen and Siddhartha Gandhi:

you guys solved my problem. It is the limit thing.
You could use an unsigned char, by the way. (0 to 255)

Apr 9 '07 #6
Tor Einar Tønnessen wrote:
ha********@gmail.com wrote:
why the following program using "char" gives me an infinite loop?
If I change char to int, it works fine.

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
for(char i=64; i<128; i++ )
cout<<i<<endl;

return 0;
}

A char has a maximum value of 127, thus the comparison (i<128) is
always true.

That's not necessarily true. The exact value for CHAR_MAX is
implementation-dependent. For instance, if char is unsigned, then it
would be larger than that. Also, if CHAR_BIT were greater than eight
the value might be larger.

Brian
Apr 9 '07 #7
On Apr 9, 12:56 am, "Default User" <defaultuse...@yahoo.comwrote:
Tor Einar Tønnessen wrote:
haijin....@gmail.com wrote:
why the following program using "char" gives me an infinite loop?
If I change char to int, it works fine.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
for(char i=64; i<128; i++ )
cout<<i<<endl;
return 0;
}
A char has a maximum value of 127, thus the comparison (i<128) is
always true.

That's not necessarily true. The exact value for CHAR_MAX is
implementation-dependent. For instance, if char is unsigned, then it
would be larger than that. Also, if CHAR_BIT were greater than eight
the value might be larger.

Brian
But in the context of the problem he has presented, that would not
matter. He's guaranteed the range that he's asking for. (C99 Annex E)

Apr 9 '07 #8
Siddhartha Gandhi wrote:
On Apr 9, 12:56 am, "Default User" <defaultuse...@yahoo.comwrote:
>Tor Einar Tønnessen wrote:
>>haijin....@gmail.com wrote:
>>>why the following program using "char" gives me an infinite loop?
If I change char to int, it works fine.
>>>#include <iostream>
using namespace std;
>>>int main(int argc, char** argv)
{
for(char i=64; i<128; i++ )
cout<<i<<endl;
>>>return 0;
}
>>A char has a maximum value of 127, thus the comparison (i<128) is
always true.

That's not necessarily true. The exact value for CHAR_MAX is
implementation-dependent. For instance, if char is unsigned, then it
would be larger than that. Also, if CHAR_BIT were greater than eight
the value might be larger.

Brian

But in the context of the problem he has presented, that would not
matter. He's guaranteed the range that he's asking for. (C99 Annex E)
What range is that? And what does the range have to do with the fact
that in the OP's case the value of 'i' can *never* reach 128 (which
is required to provide the failure of the condition and thus exiting
the loop)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 9 '07 #9
On Apr 9, 4:09 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Siddhartha Gandhi wrote:
On Apr 9, 12:56 am, "Default User" <defaultuse...@yahoo.comwrote:
Tor Einar Tønnessen wrote:
haijin....@gmail.com wrote:
>>why the following program using "char" gives me an infinite loop?
If I change char to int, it works fine.
>>#include <iostream>
using namespace std;
>>int main(int argc, char** argv)
{
for(char i=64; i<128; i++ )
cout<<i<<endl;
>>return 0;
}
>A char has a maximum value of 127, thus the comparison (i<128) is
always true.
That's not necessarily true. The exact value for CHAR_MAX is
implementation-dependent. For instance, if char is unsigned, then it
would be larger than that. Also, if CHAR_BIT were greater than eight
the value might be larger.
Brian
But in the context of the problem he has presented, that would not
matter. He's guaranteed the range that he's asking for. (C99 Annex E)

What range is that? And what does the range have to do with the fact
that in the OP's case the value of 'i' can *never* reach 128 (which
is required to provide the failure of the condition and thus exiting
the loop)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask.
Hm, I retract my comments, unsigned char would not suffice.

Apr 9 '07 #10

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

Similar topics

11
by: Denis Hierstein | last post by:
I need a function, witch make a break in a for-loop and wait for the <enter>-key ... when I use Pascal I just use the Read; or the ReadLn;-function, then the loop stop as long as the user push the...
37
by: Debajit Adhikary | last post by:
Now I know (from Stroustrup's site) that "char" is pronounced "tchar" and not "kar". Does the same hold true largely in the C world?
13
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
9
by: Gary | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of...
6
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
5
by: Omats.Z | last post by:
what is meaning os "char **option meet"? I get a function like this: __________________________________ int getchoice(char *greet, char *choices) { int chosen = 0; int selected; char **option;...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
1
by: Manoj Jangid | last post by:
Hi how do i convert wchar_t to char or vice versa Kind Regards
14
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
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: 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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.