Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are correct, if the function would return a word, it'd be rather <code>char *getword()</code>. However, according to K&amp;R</p> <blockquote> <p>The function value is the first character of the word, or EOF for end of file, or the character itself if it is not alphabetic</p> </blockquote> <p>Returning an <code>int</code> is ok, as in C, a character is like an <code>int</code> having only 8 bits, in the [-128, +127] range.</p> <p>So where the word is returned?<br> In the <code>char *word</code> given as parameter. Initially <code>char *w</code> gets a copy of the <code>word</code> pointer, and then the characters read are set into the memory pointed to by <code>w</code>.</p> <p>Having <code>"in "</code> in the input buffer, <code>isspace</code> would return false, and <code>c</code> is assigned the non-space character. Then, <code>*w++</code> put that character at position [0] of word (<em>i</em>) increments the <code>w</code> pointer (<code>++</code>). <code>word[0]</code> contains 'i'.</p> <p>The <code>!isalpha</code> test is false, thus that part is skipped.</p> <p>Then characters are read from the input and stored into the next <code>w</code> position, until a non alphanumeric entry is read (or limit <code>lim</code> is reached) - in this non-alphanumeric case, the character read is actually put back into the input buffer, and <code>w</code> - which contains that undesired char - is not incremented (due to <code>break</code>). Then the following <code>*w = '\0'</code> overwrites that non-alpha char, and "close" the C string (in C strings ends with a character having a 0 value).</p> <p>In your example, that stores 'n' in <code>w</code>, increments <code>w</code>, then stores ' ' into <code>w</code> and performs the code for <code>!isalnum</code>, i.e. breaks the loop. Then since <code>w</code> was not incremented after storing ' ', the <code>*w = '\0'</code> replaces the space, and "closes" the string.</p> <p>[<em>the other half of the question has already been answered by someone else</em>]</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. 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