0

I have seen several posts in regards to the Character Counting section but none of them answers the question to not displaying the output correctly even after using Ctrl+D to exit.

#include <stdio.h>

int main() {
    long nc;
    nc = 0;
    while (getchar() != EOF) {
        ++nc;
    }
    printf("%ld\n", nc); 
}

After running this enter image description here

It doesn't count the characters just exit. What am I doing incorrectly? This is done in CLion on Mac

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • What happens if you replace `%ld\n` with `\n%ld\n`? What happens if you run it from the terminal instead of CLion? What happens if you hit Enter before you hit Ctrl+D? – Joseph Sible-Reinstate Monica May 10 '20 at 18:26
  • How are you providing the input to the program? If you're typing at a terminal window and not typing a newline before typing control-D, how are you making the program stop? Basically, you have to type control-D twice to get the program to stop — or once if you type the control-D immediately after a newline (or before typing any other characters). You may be suffering from one of the peculiarities of command-line programs run from within an IDE (such as CLion). – Jonathan Leffler May 10 '20 at 18:26
  • Try inserting a newline prior to entering ctrl-d – lane.maxwell May 10 '20 at 18:30
  • Just typing that ctrl+D exits it and doesn't print. In theory it should print after exiting the loop, but it isn't – user9240544 May 10 '20 at 18:30
  • You should do a `return 0;` at the bottom of `main`. Otherwise, the exit code could be random. But, I downloaded/compiled/ran your program on my PC/linux system. I _do_ get the output of the `printf`, thus, I am unable to reproduce your problem, but I invoked with `./program < /etc/passwd`. You could do: `./program < /dev/null` and _still_ should get some output. Or, the only reason you might not get output is your program is segfaulting, which can _not_ happen for _your_ program as written. It seems you are using a `hi` command to invoke your program (`hello`)??? That may be the issue. – Craig Estey May 10 '20 at 18:31
  • 1
    @CraigEstey `main` is the one function that defaults to `return 0` when omitted. Please see [this answer](https://stackoverflow.com/a/19293663/4142924). – Weather Vane May 10 '20 at 18:35
  • Does this answer your question? [C input - getchar()](https://stackoverflow.com/questions/9796054/c-input-getchar) – Schwern May 10 '20 at 18:41
  • @WeatherVane Good to know, thanks. I've always added an explicit `return`, so I wasn't aware of this. I should have disassembled the executable with `objdump` to verify. But, it makes sense. It cuts down on the amount of "help traffic" for "why does my hello world program return random exit status?" – Craig Estey May 10 '20 at 18:41
  • @CraigEstey yes, and unlike other non-void functions, there is no compiler warning. – Weather Vane May 10 '20 at 18:52
  • Quite late but I thought of sharing my findings. It seems it's an issue with CLion, check [this.](https://youtrack.jetbrains.com/issue/CPP-5704). I found about this from [this answer.](https://stackoverflow.com/a/45949041/6511525) I have tested the code. It behaves as @JonathanLeffler said in comment. [Here](https://stackoverflow.com/a/9796240) is a nice and brief explanation of ctrl+D(unix) and ctrl+z(windows) pressing effects. – Jaiaid Mobin Mar 03 '21 at 15:00

0 Answers0