📄 sizeof.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>sizeof</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C/C++ Keywords</a> > <a href= "sizeof.html">sizeof</a> </div> <div class="name-format"> sizeof </div> <p>The sizeof operator is a compile-time operator that returns the size, in bytes, of the argument passed to it. For example, the following code uses sizeof to display the sizes of a number of variables:</p> <pre class="example-code"> struct EmployeeRecord { int ID; int age; double salary; EmployeeRecord* boss; }; ... cout << "sizeof(int): " << sizeof(int) << endl << "sizeof(float): " << sizeof(float) << endl << "sizeof(double): " << sizeof(double) << endl << "sizeof(char): " << sizeof(char) << endl << "sizeof(EmployeeRecord): " << sizeof(EmployeeRecord) << endl; int i; float f; double d; char c; EmployeeRecord er; cout << "sizeof(i): " << sizeof(i) << endl << "sizeof(f): " << sizeof(f) << endl << "sizeof(d): " << sizeof(d) << endl << "sizeof(c): " << sizeof(c) << endl << "sizeof(er): " << sizeof(er) << endl;</pre> <p>When run, the above code displays this output:</p> <pre class="example-code"> sizeof(int): 4 sizeof(float): 4 sizeof(double): 8 sizeof(char): 1 sizeof(EmployeeRecord): 20 sizeof(i): 4 sizeof(f): 4 sizeof(d): 8 sizeof(c): 1 sizeof(er): 20</pre> <p>Note that sizeof can either take a variable type (such as <strong>int</strong>) or a variable name (such as <strong>i</strong> in the example above).</p> <p>It is also important to note that the sizes of various types of variables can change depending on what system you're on. Check out <a href="../data_types.html">a description of the C and C++ data types</a> for more information.</p> <p>The parentheses around the argument are not required if you are using sizeof with a variable type (e.g. sizeof(int)).</p> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="../data_types.html">C/C++ Data Types</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -