-1

I am testing out code from the book "The C programming Language" and I would like to know what is the value of EOF and how can I find it out? I tried referencing the value with a pointer of some kind but I just get a const pointer error. In anywise, below is my code and I would like to know why is there no output upon clicking enter:

#include <stdio.h>

#define IN 1 /*inside a word*/
#define OUT 0 /*outside a word*/

/* count lines, words, and characters in input*/
main()
{
  int c, nl, nw, nc, state;

  state = OUT;

  nl = nw = nc = 0;
  while ((c = getchar()) != EOF){
    ++nc;
    if (c == '/n')
      ++nl;
    if (c == ' ' || c == '\n' || c == '\t')
      state = OUT;
    else if (state == OUT){
      state = IN;
      ++nw;
    }
  }
  printf("%D %D %D\n", nl, nw, nc);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    You can find the value of `EOF` by printing it — `printf("EOF = %d\n", EOF);` — and the chances are that the value will be `-1`, but it is only guaranteed to be negative. At the terminal on Windows, you'd type control-Z to indicate EOF; on Unix, you'd type control-D. Newline (the result of hitting 'enter') is not EOF. Note that typing control-Z on Unix would probably result in the command prompt, but the program would be placed in the background in a state of suspended animation, so it wouldn't print any output until you bring it back to the foreground. Look up 'job control' for more info. – Jonathan Leffler Apr 26 '19 at 23:48
  • 3
    `if (c == '/n')` looks like a typo. `%D` also looks like a typo. – zwol Apr 26 '19 at 23:50
  • Using `%D` is not a standard `printf()` format — C is a case-sensitive language, and the format strings to `printf()` (and `scanf()`, and both their families of functions) are case-sensitive. You need `%d` three times. – Jonathan Leffler Apr 26 '19 at 23:52
  • @JonathanLeffler Why don't you write an answer instead of lengthy comments? – Swordfish Apr 26 '19 at 23:53
  • 4
    Because it takes longer to write a good answer than to write lengthy comments — @Swordfish. There are more problems in the code than what I diagnosed in my first comment. Besides, I should be looking for the duplicate, but that's a thankless task, not least because SO won't provide tools to help people find and keep records of duplicates. – Jonathan Leffler Apr 26 '19 at 23:54
  • 2
    Always compile with *warnings enabled*, and **do not** accept code until it *compiles cleanly without warning*. If you do that alone 90% of your problems will simply go away. – David C. Rankin Apr 26 '19 at 23:54
  • EOF can be any negative values although it's most commonly defined as -1. See [Actual implementation of EOF different from -1](https://stackoverflow.com/q/48583103/995714), [Is EOF always negative?](https://stackoverflow.com/q/1624051/995714), [why in c language EOF IS -1?](https://stackoverflow.com/q/4569066/995714), [C EOF symbolic value and signed character](https://stackoverflow.com/q/7058902/995714) – phuclv Apr 27 '19 at 04:52
  • **`EOF` is a signal, not a character!** – pmg Apr 27 '19 at 12:10
  • THANK you so much pmg. That is the answer I was looking for. Go ahead and post it as an answer so I can give you the best answer award. – Cameron Carter Apr 27 '19 at 12:26

1 Answers1

0
#include <stdio.h>

#define IN 1 /*inside a word*/
#define OUT 0 /*outside a word*/

/* count lines, words, and characters in input*/
int main(void)
{
  int c, nl, nw, nc, state;

  state = OUT;

  nl = nw = nc = 0;
  while ((c = getchar()) != EOF){
    ++nc;
    if (c == '\n')
      ++nl;
    if (c == ' ' || c == '\n' || c == '\t')
      state = OUT;
    else if (state == OUT){
      state = IN;
      ++nw;
    }
  }

  printf("%d %d %d\n", nl, nw, nc);
  printf("EOF = %d\n", EOF);
  return 0;
}


Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278