Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It usually takes up one byte (8 bits). The usual code I use to make sure type sizes are what I think they are follows. The sample output in the comment says my char is 1 byte (8 bits), and the same for bool.</p> <pre class="lang-cpp prettyprint-override"><code>/** * using gcc, you can compile this with the following command: * g++ -otype-sizes type_sizes.cpp * and then run with with * ./type-sizes * * output on my 64bit linux machine follows. Note that * the not-so-primitive types are reporting size on * the stack (the actual data in on the heap and is * not reported by sizeof()). To get the "length" of * these you can use vector&lt;&gt;::size() or string::length(). bits in a single char: 8 Sizes of primitive types: char: 1 bool: 1 short: 2 int: 4 long: 8 long long: 8 float: 4 double: 8 long double: 16 Not so primitive types: string(""): 8 string("Hello, World!"): 8 vector&lt;int&gt;(0): 24 vector&lt;int&gt;(10): 24 * **/ #include &lt;climits&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; int main() { cout &lt;&lt; "bits in a single char: " &lt;&lt; CHAR_BIT &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Sizes of primitive types:\n" &lt;&lt; " char: " &lt;&lt; sizeof(char) &lt;&lt; endl &lt;&lt; " bool: " &lt;&lt; sizeof(bool) &lt;&lt; endl &lt;&lt; " short: " &lt;&lt; sizeof(short) &lt;&lt; endl &lt;&lt; " int: " &lt;&lt; sizeof(int) &lt;&lt; endl &lt;&lt; " long: " &lt;&lt; sizeof(long) &lt;&lt; endl &lt;&lt; " long long: " &lt;&lt; sizeof(long long) &lt;&lt; endl &lt;&lt; " float: " &lt;&lt; sizeof(float) &lt;&lt; endl &lt;&lt; " double: " &lt;&lt; sizeof(double) &lt;&lt; endl &lt;&lt; " long double: " &lt;&lt; sizeof(long double) &lt;&lt; endl &lt;&lt; endl &lt;&lt; " Not so primitive types:\n" &lt;&lt; " string(\"\"): " &lt;&lt; sizeof(string("")) &lt;&lt; endl &lt;&lt; " string(\"Hello, World!\"): " &lt;&lt; sizeof(string("Hello, World!")) &lt;&lt; endl &lt;&lt; " vector&lt;int&gt;(0): " &lt;&lt; sizeof(vector&lt;int&gt;(0)) &lt;&lt; endl &lt;&lt; " vector&lt;int&gt;(10): " &lt;&lt; sizeof(vector&lt;int&gt;(10)) &lt;&lt; endl &lt;&lt; endl; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload