0

I follow the book C programing second edition and try to output this code:

#include <stdio.h>
main()
{
    long nc;
    nc = 0;

    while(getchar() != EOF)
        ++nc;
    printf("%1d\n", nc);
}

and I can't figure out why I don't have any output.

I work on crunchbang++ and to create the output I entered in the terminal:

cc -ansi file.c

then

./a.out

but when I enter characters I have a blank response.

I don't want just the working code, but an explanation, because I really want to learn how it works.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
leyvvs
  • 11
  • 2
  • 2
    I would guess that the program doesn't even compile since `main()` isn't valid. It should be `int main(void)` – Ted Lyngmo Feb 06 '23 at 17:42
  • 2
    How do you stop the program? CTRL-c? CTRL-d? CTRL-z? – Support Ukraine Feb 06 '23 at 17:44
  • 2
    _"why assigning nc to 0 where long nc is that by default"_ - It's not `0` by default. It has an indeterminate value. – Ted Lyngmo Feb 06 '23 at 17:45
  • 5
    `%1d` is not the same as `%ld`. – Oka Feb 06 '23 at 17:46
  • 2
    Typo: `printf("%1d\n", nc);` should be `printf("%ld\n", nc);`. Compiler warnings should show this up. – Weather Vane Feb 06 '23 at 17:46
  • @TedLyngmo I guess most compilers will in fact compile the program despite the wrong `main` – Support Ukraine Feb 06 '23 at 17:46
  • Hi @TedLyngmo. That is why I put the -ansi to compile it propely but it even with int main(void) it does the same. – leyvvs Feb 06 '23 at 17:48
  • 1
    The loop `while(getchar() != EOF)` will on normal systems never end. You need to emulate EOF. That's most likely either ctrl-d or ctrl-z – Support Ukraine Feb 06 '23 at 17:48
  • @SupportUkraine it will if the input is redirected. The `long` count is a clue, imagine typing that many characters! – Weather Vane Feb 06 '23 at 17:49
  • @WeatherVane Well... true... but there is no mentioning of redirect – Support Ukraine Feb 06 '23 at 17:51
  • @leyvvs How do you run the program? Does it stop by itself? What exactly happens? – Support Ukraine Feb 06 '23 at 17:52
  • 1
    What @SupportUkraine asked is important: How do you end the input? What do you press when you expect to see the result of the count? – Ted Lyngmo Feb 06 '23 at 17:52
  • See: [End of File (EOF) in C](https://stackoverflow.com/q/4358728/2505965) – Oka Feb 06 '23 at 17:53
  • Yes @SupportUkraine it nevers ends I have do ctrl D to stop it. I follow the book "C progtamming snd edition" they put it like this. – leyvvs Feb 06 '23 at 17:57
  • 1
    @TedLyngmo you just gave me some homework lol cool. I have no clue what they mean. Will do some researches. – leyvvs Feb 06 '23 at 17:58
  • In Windows the Ctrl-Z must be preceded and followed by a newline, can't say for Linux Ctrl-D. – Weather Vane Feb 06 '23 at 17:58
  • So, when you press ctrl-d does the program actually end without printing the count? Doesn't it end at all? If not, try pressing enter first, then ctrl-d. – Ted Lyngmo Feb 06 '23 at 17:58
  • @JohnBollinger Correct. `std=c++18` was a typo. It doesn't even exist :-) I removed that comment – Ted Lyngmo Feb 06 '23 at 18:14
  • What do you mean by "intermediate value" @TedLyngmo ? Is it context dependent or something like that ? – leyvvs Feb 07 '23 at 23:57
  • I made some research on EOF. It was obscur at the beginning but I finally get it. Yeap I had to stop the command with CTRL-D then the result appear. That was so cool. Thanks to all of you guys, really cool of you all. – leyvvs Feb 08 '23 at 00:09
  • @leyvvs [What is Indeterminate value?](https://stackoverflow.com/questions/13423673/what-is-indeterminate-value) - so if you had not done `nc = 0;` before `++nc;` the program would have had undefined behavior. – Ted Lyngmo Feb 08 '23 at 07:48

1 Answers1

0
#include <stdio.h>

int main(void)
{
    long num_of_chars = 0;

    while(getchar() != '\n'){
        ++num_of_chars;
    }

    printf("%ld\n", num_of_chars);
}

EOF means End of File.
EOF is not a character. It is a state which indicates no more characters to read from a file stream. When you enter EOF command from the terminal, you are signalling the OS to close the input stream, not putting in a special character.

Now either you can change your code to read a file, emulate EOF, or cancel while loop on different character.

Above example will end while loop on new line.

$ ./main
This is a test
14

Edit:
Tested on linux with EOF.
Ctrl + Z will outright stop the program.
Ctrl + D will output num_of_chars if pressed twice or by doing Enter and then Ctrl + D.

Edit 2:
With EOF.
printf foo | ./your_program Output: 3
echo foo | ./your_program Output: 4 (echo adds newline caharacter)

Flewz
  • 343
  • 9
  • Thanks for your answer @Flewz. As I can see the code is different from the one from the book. Does it means that the book's syntax isn't used anymore ? – leyvvs Feb 07 '23 at 23:54
  • @leyvvs If by syntax you mean your `main` declaration then yes. Every function must be declared to return something - or be `void`. The `main` function is special though and should always be declared to return `int` (except perhaps in some freestanding implementation). From what year is that book? – Ted Lyngmo Feb 08 '23 at 08:00
  • @Flewz Changing the test to check for `'\n'` will make it only count the first line entered so you changed what the program is actually doing. Your program will also hang if EOF is actually reached without a `\n` ever being read. Test this on your linux: `printf foo | ./your_program` ... it will never stop running. – Ted Lyngmo Feb 08 '23 at 08:05
  • @TedLyngmo the book is from 1988 it is the C programming book second edition. – leyvvs Feb 08 '23 at 16:57
  • @leyvvs Oh my ... and it uses `main` without `int` (or anything else) in the declaration? Time to get a new book. It sounds harmful to learn from that oldie. – Ted Lyngmo Feb 08 '23 at 16:59
  • @TedLyngmo it is a C programmer who recommended me this book. It is the creators of C that wrote it. – leyvvs Feb 08 '23 at 17:06
  • @leyvvs That doesn't matter since the book contains stuff that doesn't fly Today. Why learn from an outdated source? – Ted Lyngmo Feb 08 '23 at 17:07
  • @TedLyngmo Do you have a good recommendation book to learn from? – leyvvs Feb 08 '23 at 17:17
  • Here could be a good place to start: [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) - Do read the comments first though. Your book is there with the comment _"...written by the inventor of C. However, the language has changed and good C style has developed in the last 25 years, and there are parts of the book that show its age."_. Look for one that covers _at least_ C11. We are at C17/18 now and C23 is just around the corner. – Ted Lyngmo Feb 08 '23 at 17:32
  • 1
    Thanks for your recommendations @TedLyngmo. – leyvvs Feb 12 '23 at 14:48