Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>size_t</code> is an unsigned type. So, it cannot represent any negative values(&lt;0). You use it when you are counting something, and are sure that it cannot be negative. For example, <a href="http://devdocs.io/c/string/byte/strlen" rel="noreferrer"><code>strlen()</code></a> returns a <code>size_t</code> because the length of a string has to be at least 0.</p> <p>In your example, if your loop index is going to be always greater than 0, it might make sense to use <code>size_t</code>, or any other unsigned data type.</p> <p>When you use a <code>size_t</code> object, you have to make sure that in all the contexts it is used, including arithmetic, you want non-negative values. For example, let's say you have:</p> <pre><code>size_t s1 = strlen(str1); size_t s2 = strlen(str2); </code></pre> <p>and you want to find the difference of the lengths of <code>str2</code> and <code>str1</code>. You cannot do:</p> <pre><code>int diff = s2 - s1; /* bad */ </code></pre> <p>This is because the value assigned to <code>diff</code> is always going to be a positive number, even when <code>s2 &lt; s1</code>, because the calculation is done with unsigned types. In this case, depending upon what your use case is, you might be better off using <code>int</code> (or <code>long long</code>) for <code>s1</code> and <code>s2</code>.</p> <p>There are some functions in C/POSIX that could/should use <code>size_t</code>, but don't because of historical reasons. For example, the second parameter to <code>fgets</code> should ideally be <code>size_t</code>, but is <code>int</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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