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);
}

Recommended Answers

All 2 Replies

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'.

thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.