For loop writing special signs
void printchars()
{
for (x=128;x<224;x++)
write(x);
I want the x to be a char in the write function. How can i change the x to be treated by the write functions as a char, but an i开发者_如何学编程nt in the loop?
What is the point of making x
an int
if you're just going to strip away its range? That's what makes this a very strange request. You should just make x
a unsigned char
-- for(unsigned char x = 128; x <224; ++ x) { ...
.
If you just want to ensure you're calling the unsigned char
template specialization of write<>
, then call it like this:
write<unsigned char>(x);
If not, then you will have to use type casting:
write((unsigned char)x);
Edit: I just realized what you might be experiencing. My guess is that you originally used char
but found something wrong with numbers over 127. You should probably be using unsigned char
for x
instead of either int
or char
. I edited my answer to accommodate this. char
has a range of -128 to +127. unsigned char
has a range of 0-255.
Cast x to a char:
write(static_cast<char>(x));
Note that it is ok for x to be a char as the loop counter as well.
精彩评论