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

H

haijin.biz

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;
}
 
S

Siddhartha Gandhi

In GCC, a warning tells you exactly what you need to know:

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

Tor Einar =?UTF-8?B?VMO4bm5lc3Nlbg==?=

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.
 
S

Siddhartha Gandhi

#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).
 
H

haijin.biz

Thanks Einar Tønnessen and Siddhartha Gandhi:

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

Siddhartha Gandhi

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)
 
D

Default User

Tor said:
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
 
S

Siddhartha Gandhi

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)
 
V

Victor Bazarov

Siddhartha said:
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
 
S

Siddhartha Gandhi

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

Hm, I retract my comments, unsigned char would not suffice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top