第4章 表达式
Posted COCO_PEAK_NOODLE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第4章 表达式相关的知识,希望对你有一定的参考价值。
1-三目运算符的写法
int main()
{
for (unsigned g; cin >> g; )
{
// conditional operators
auto result = g > 90 ? "high pass" : g < 60 ? "fail" : g < 75 ? "low pass" : "pass";
cout << result << endl;
// if statements
if (g > 90) cout << "high pass";
else if (g < 60) cout << "fail";
else if (g < 75) cout << "low pass";
else cout << "pass";
cout << endl;
}
return 0;
}
2-类型占用字节
int main()
{
// by using method below only include what is needed.
using std::cout;
using std::endl;
// void type
cout << "void: nullptr_t\\t" << sizeof(std::nullptr_t) << " bytes" << endl << endl;
// boolean type
cout << "bool:\\t\\t" << sizeof(bool) << " bytes" << endl << endl;
// charactor type
cout << "char:\\t\\t" << sizeof(char) << " bytes" << endl;
cout << "wchar_t:\\t" << sizeof(wchar_t) << " bytes" << endl;
cout << "char16_t:\\t" << sizeof(char16_t) << " bytes" << endl;
cout << "char32_t:\\t" << sizeof(char32_t) << " bytes" << endl << endl;
// integers type
cout << "short:\\t\\t" << sizeof(short) << " bytes" << endl;
cout << "int:\\t\\t" << sizeof(int) << " bytes" << endl;
cout << "long:\\t\\t" << sizeof(long) << " bytes" << endl;
cout << "long long:\\t" << sizeof(long long) << " bytes" << endl << endl;
// floating point type
cout << "float:\\t\\t" << sizeof(float) << " bytes" << endl;
cout << "double:\\t\\t" << sizeof(double) << " bytes" << endl;
cout << "long double:\\t" << sizeof(long double) << " bytes" << endl << endl;
// Fixed width integers
cout << "int8_t:\\t\\t" << sizeof(int8_t) << " bytes" << endl;
cout << "uint8_t:\\t" << sizeof(uint8_t) << " bytes" << endl;
cout << "int16_t:\\t" << sizeof(int16_t) << " bytes" << endl;
cout << "uint16_t:\\t" << sizeof(uint16_t) << " bytes" << endl;
cout << "int32_t:\\t" << sizeof(int32_t) << " bytes" << endl;
cout << "uint32_t:\\t" << sizeof(uint32_t) << " bytes" << endl;
cout << "int64_t:\\t" << sizeof(int64_t) << " bytes" << endl;
cout << "uint64_t:\\t" << sizeof(uint64_t) << " bytes" << endl;
return 0;
}
输出
void: nullptr_t 8 bytes
bool: 1 bytes
char: 1 bytes
wchar_t: 4 bytes
char16_t: 2 bytes
char32_t: 4 bytes
short: 2 bytes
int: 4 bytes
long: 8 bytes
long long: 8 bytes
float: 4 bytes
double: 8 bytes
long double: 16 bytes
int8_t: 1 bytes
uint8_t: 1 bytes
int16_t: 2 bytes
uint16_t: 2 bytes
int32_t: 4 bytes
uint32_t: 4 bytes
int64_t: 8 bytes
uint64_t: 8 bytes
以上是关于第4章 表达式的主要内容,如果未能解决你的问题,请参考以下文章