3.4 sizeof, comma, and arithmetic if operators

from http://www.learncpp.com/cpp-tutorial/34-sizeof-comma-and-arithmetic-if-operators/

sizeof

Operator Symbol Form Operation
Sizeof sizeof sizeof(type)
sizeof(variable)
Returns size of type or variable in bytes

sizeof操作符返回以字节为单位的变量或是类型的大小。

如:

   1: #include <iostream>
   2:  
   3: int main()
   4: {
   5:     using namespace std;
   6:     cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl;
   7:     cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
   8:     cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
   9:     cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
  10:     cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
  11:     cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
  12:     cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
  13:     cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
  14:     cout << "long double:\t" << sizeof(long double) << " bytes" << endl;
  15:     return 0;
  16: }

 

 

comma

Operator Symbol Form Operation
Comma , x, y Evaluate x then y, return value of y

逗号操作符返回的是最右边的操作数。

   1: int x = 0;
   2: int y = 2;
   3: int z = (++x, ++y); // increment x and y
   4: cout << z << endl;

结果为

3

一般情况下不使用逗号操作符

 

Arithmetic if

Operator Symbol Form Operation
Arithmetic if ?: c ? x : y If c is nonzero (true) then evaluate x, otherwise evaluate y
if (condition)

    x = some value

else

    x = some other value

等同于

x = (condition) ? some value : some other value;

你可能感兴趣的:(sizeof)