0

Quick question: what does "if(c == '\n') mean in this context? Is it just using '\n' as a reference for a numeric value within ASCII? which in that case would be 10, so if 10 is entered, this is when nl increments up one?

{
  int c, nl;
  nl = 0;
  while ((c = getchar()) != EOF)
      if (c == '\n')
          ++nl;
  printf("%d\n", nl);
}

Edited by Reverend Jim: Fixed formatting

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by rickymak
0

what does "if (c=='\n')" mean in this context?

It checks if the variable c contains the value of the newline character '\n'.

so if 10 is entered, this is when nl increments up one?

No, if a newline character occurs in the stream you're reading from, then nl increments up one. If you enter 10, then it is not considered as the ASCII value 10, but as two separate characters '1' and '0'.

Edited by mvmalderen: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.