title: Why should string length be plus one its capacity in C?
body: <p>Your string length should be one more than the maximum number of characters you want it to be able to hold. Logical enough: strings are terminated with a <code>NULL</code> character.</p>
  
  <p>It's a very general advice that most newbies get. However, as I grew in programming, now it seems that it's not so correct. </p>
  
  <p>The indexing of <em>any</em> type of array, be it <code>int</code> or <code>char</code>, starts from <code>0</code>. The maximum index value of most arrays, therefore, is one less than its numerical value. It's same with a string, but since it has an extra character at the end, it gets incremented by one. So, the string length is the same as the number of characters in it.</p>
  
  <hr>
  
  <p>To see if I'm right, see this snippet:</p>
  
  <pre><code>char str[9];
  scanf("%s", str);
  printf("%d", strlen(str));
  </code></pre>
  
  <p>Make this a full-fledged program, and run it. Type <code>123456789</code>, a guaranteed 9-character long text, and see the results. It could hold the string and sure enough, the string length is <code>9</code>. </p>
  
  <hr>
  
  <p>I even witnessed many expert programmers saying that string size should be plus one its capacity. Is this advice largely a myth, or I am going wrong somewhere?</p>
  
  <h2>EDIT</h2>
  
  <p>Let's say I want to create an integer array <code>Arr</code> that can hold <code>x</code> number of elements. The index value of <code>Arr</code>'s last element will be one less than <code>x</code> since index values start from <code>0</code> and not <code>1</code>. So, its length is <code>x-1</code>.</p>
  
  <p>How would you declare it then? I'd do that like this: <code>int Arr[x-1];</code>. I don't think there's any issues with this. </p>
  
  <p>Now if <code>Arr</code> were a <code>char</code> type array (i.e. a string), the length of <code>Arr</code> would be one more than that of its <code>int</code> counterpart since it has an extra NULL character at the end. This will end up as: <code>(x-1)+1=x</code>. </p>
  
  <p><a href="https://stackoverflow.com/questions/43424481/string-size-should-be-plus-one-its-capacity/43424746#43424746">Code to demonstrate this</a></p>
  
  <p>So why does the declaration this time has to be <code>char Arr[x+1]</code> and not simply <code>char Arr[x]</code>?</p>