 <p>First, you need to know <em>What is a string in C?</em>. A string is an array of non-zero bytes (<code>char</code>), usually printable characters, which terminate in a null (zero, represented by the character <code>'\0'</code>).</p> <p>Then, you need to know how a <code>for</code> loop works:</p> <pre><code>for ( initial condition ; test condition ; loop expression ) { // some code } // Code after the loop </code></pre> <p>The <code>for</code> loop will:</p> <ol> <li>Execute <em>initial condition</em></li> <li>Execute <em>test condition</em> and, if it's TRUE (non-zero) will execute <em>some code</em>. Otherwise, if it's FALSE (zero) it will EXIT the loop and go to the <em>Code after the loop</em>.</li> <li>Execute <em>loop expression</em></li> <li>Go to 2</li> </ol> <p>With the basics out of the way, let's look at the function given by the teacher:</p> <pre><code>int length(char *word) { int i; for (i = 0; word[i] != '\0'; i++) { { } } return i; } </code></pre> <p>The meat of the function is the <code>for</code> loop: It then goes through the <code>for</code> loop steps:</p> <ol> <li>Initially, <code>i</code> is set to <code>0</code>. <code>i</code> represents the index into the string (array of <code>char</code>).</li> <li>Check if <code>word[i] != '\0'</code> (in other words, if this is TRUE, then it's not the end of the string). There's no code inside the loop to execute, so we go right to step 3.</li> <li>The <em>loop expression</em> is executed, which is incrementing <code>i</code>, the string (<code>char</code> array) index, meaning we are now pointing to the next character in the string.</li> </ol> <p>If your word is, "cat", then the array is <code>{ 'c', 'a', 't', '\0' }</code>. So the loop steps will do this:</p> <ol> <li>Set <code>i</code> to <code>0</code>.</li> <li>Check <code>word[0] != '\0'</code>, which checks <code>'c' != '\0'</code>. This is TRUE because <code>'c'</code> is NOT equal to <code>'\0'</code>.</li> <li>Take <code>i++</code> (increment <code>i</code>), so now <code>i</code> is <code>1</code>.</li> <li>Check <code>word[1] != '\0'</code>, which checks <code>'a' != '\0'</code>. This is TRUE because <code>'a'</code> is NOT equal to <code>'\0'</code>.</li> <li>Take <code>i++</code> (increment <code>i</code>), so now <code>i</code> is <code>2</code>.</li> <li>Check <code>word[2] != '\0'</code>, which checks <code>'t' != '\0'</code>. This is TRUE because <code>'t'</code> is NOT equal to <code>'\0'</code>.</li> <li>Take <code>i++</code> (increment <code>i</code>), so now <code>i</code> is <code>3</code>.</li> <li>Check <code>word[3] != '\0'</code>, which checks <code>'\0' != '\0'</code>. This is FALSE because <code>'\0'</code> is equal to <code>'\0'</code>.</li> <li>The loop ends now because the <em>test condition</em> has become FALSE.</li> </ol> <p>What's the value of <code>i</code> at the end of the loop? It's <code>3</code>, which is the length of the string because we kept stepping through the string, one character at a time, incrementing <code>i</code> from <code>0</code> until we hit the spot after the last character of the string, which is <code>'\0'</code>.</p> <p>The function then returns <code>i</code>, the length of the string.</p> <p>HTH</p>