不显示字符变量值
请原谅模糊标题(我不知道如何解决问题)。无论如何,在我的代码中,我明确地声明了几个变量,其中两个是有符号/无符号的int变量,其他符号/无符号char类型变量。不显示字符变量值
我的代码:
#include <iostream>
int main(void)
{
unsigned int number = UINT_MAX;
signed int number2 = INT_MAX;
unsigned char U = UCHAR_MAX;
signed char S = CHAR_MAX;
std::cout << number << std::endl;
std::cout << "The size in bytes of this variable is: " << sizeof(number) << std::endl << std::endl;
std::cout << number2 << std::endl;
std::cout << "The size in bytes of this variable is: " <<sizeof(number2) << std::endl << std::endl;
std::cout << U << std::endl;
std::cout << "The size in bytes of this variable is: " << sizeof(U) << std::endl
<< std::endl;
std::cout << S << std::endl;
std::cout << "The size in bytes of this variable is: " <<sizeof(S) << std::endl << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
对不起代码是经过加密的,由于过度的长度,但我的问题是,我焦炭变量不是“印刷”到我的输出。它以字节为单位输出它们的大小,但无论我做什么,我似乎都无法让它工作。另外,第二个char变量(signed(S))打印看上去像三角形的东西,但没有其他东西。
尝试这种情况:
std::cout << (int)U << std::endl;
std::cout << "The size in bytes of this variable is: " << sizeof(U) << std::endl
<< std::endl;
std::cout << (int)S << std::endl;
std::cout << "The size in bytes of this variable is: " <<sizeof(S) << std::endl << std::endl;
的解释是如此简单:当类型为char
,cout
试图产生一个符号输出是whitespace
为255或127。当一个相当状三角类型是int
,cout只是输出变量的值。例如在C中:
printf("%d", 127) // prints 127
printf("%c", 127) // prints triangle, because %c formatter means symbolic output
这个答案不能比现在更进一步改进。做得好! – Jake2k13
大的修改,最好是这样投射:static_cast
在你的情况下,它们[相同](http://stackoverflow.com/questions/103512/in-c-why-use-static-castintx-instead-of-intx)。 – Netherwire
它们被打印但你看不到它。 你可以打开“limits.h中”的文件:
#define CHAR_BIT 8 /* number of bits in a char */
#define SCHAR_MIN (-128) /* minimum signed char value */
#define SCHAR_MAX 127 /* maximum signed char value */
#define UCHAR_MAX 0xff /* maximum unsigned char value */
,那么你在ASCII表中查找UCHAR_MAX和CHAR_MAX,
扮演他们为int要显示的值,而不是ASCII字符。 –