What's the role of c != '\n' condition in "for" loop (C language)?

Viewed 322

The input of this function is s[] - char array and lim - max possible length of this array.

The function itself is used to determine the length of an inputed char array which is entered in the console.

The question is, what's the main idea of c != '\n' condition in for loop?

I guess it's used to break a loop. It's quite clear. But I can't get how it can be implemented if I don't type \n in my input.

Is that a terminator at the end of an array like \0?

If that's the case why should we use the if (c == '\n') condition after that?

The code:

int getline(char s[],int lim)
{
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}
6 Answers

As you press Enter a \n newline character is added to the input buffer, stdin, this means that this is the last character in the input buffer.

So it's only fitting the cycle continues on until getchar() retrieves this last character.

The cycle control states that c must be:

  • Different from \n.
  • Different fromEOF.
  • And the iterator i must be lower than lim - 1.

If one of these conditions is not met the cycle breaks, so it's possible that c is not equal to \n.

After that, if c is equal to \n, it will be added to s, the condition if (c == '\n') is there because, as stated, c might not be equal to \n, and in that case it shouldn't be added to s.

Lastly s must be null terminated (s[i] = '\0') so it can be properly interpreted as a string, aka a null terminated char array. \n is not a null terminator, \0 is.

The fact that \n is added is only because the implementer wanted it to be so, it's part of the implementation, it wouldn't have to be, but it is. It can be useful in some cases, for instance fgets library function has a similar implementation, it adds \n to the containing buffer and then null terminates it.

Well you want to get a line. \0 terminates the string but a string can exist of multiple lines. As the function's name indicates you only want a single line. Lines are terminated with \n and after that a new line begins.

The condition

c != '\n'

is evaluated as true if a character different to new line character is encountered.

As you can understand, since it is a getline() implementation, it is a very good reason to stop reading from the input string.

Please note how, after this check ends the loop s[i] = '\0'; is executed in order to terminate the string one position after where previously the new line character was.


So, how do getline() works?

The core of that function is the for-loop

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)

The condition i < lim-1 && (c=getchar())!=EOF && c!='\n' means: "Assign to c a character got with `getchar() until...":

  • the read character is EOF
  • the read character is \n (newline)
  • the number of characters requested by the caller (lim) has been read

Because '\n'it is only one of the conditions - it can be also EOF which author does not want to save and also chars number limit can be reached and we do not want to write the nul terminator out of the char array bounds

The nul termination is not related to any of those conditions and it always happens

If the character read c is a return to new line \n, then the for loop will stop, and the array will end with \n followed by \0.

And that's what the function is supposed to do, it's called getline(), and lines end with \n.

The logic is simply this:

In the for loop we're saying that when the user hit Enter, stop reading; so it stops and won't read the rest of the input. because the condition c!='\n' becomes false.

In the if statement, we're telling the program to continue reading; so "if" the user hits Enter, the program will start reading the rest of the input from where it left off and will start looping again.

The if statement is to prevent the loop from stopping and also a way of telling the program that there are separate lines in the input.

Related