2

I have a very simple C example program that does a crude count of characters words and spaces from input. The program compiles without error but when tested the program doesn't return any of the int variables via the print function. I am using VS2012 for coding and compiling. Stepping into the code shows that the values are being calculated correctly. Is there something wrong with my code or the compiler?

#include <stdio.h>

#define IN 1
#define OUT 0
/* count digits, white space, others */
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);
}
rlwheeler
  • 518
  • 4
  • 12
  • 1
    Are you sure you have output generated to a valid log or window and are looking in the correct place for the output? Also, on *rare* occasions, output is not flushed, and you should flush explicitly when in doubt. Are you running this code from the command line? If so it should print. Otherwise if a GUI or something there is probably another mechanism for displaying results. – clearlight Mar 27 '15 at 18:22
  • 3
    Did you break the loop with `EOF`? – P.P Mar 27 '15 at 18:23
  • Ah, good point - can he see ANY printf() output in other words. Something I would have determined right at the outset if I were coding it. – clearlight Mar 27 '15 at 18:25
  • 1
    @rlwheeler Run your program pressing Ctrl + F5. Also you have to press Enter before pressing Ctrl + z – Vlad from Moscow Mar 27 '15 at 18:25
  • 1
    Works fine for me. Run it, type some stuff, press ctrl-z, see output. – Retired Ninja Mar 27 '15 at 18:35
  • "Stepping into the code shows that the values are being calculated correctly" Minor: disagree about line count. IMO, if input was only "123" or "123\n", both would be 1 line. I would count lines if the _previous_ character was `'\n'` and initialize the previous variable with `'\n'. – chux - Reinstate Monica Mar 27 '15 at 19:15
  • Note: In C, the 6 standard (C locale) "white-spaces" are `" \f\n\r\t\v"`. – chux - Reinstate Monica Mar 27 '15 at 19:18

3 Answers3

0

It works if you run it with stdin coming from a file a.exe < test.txt. It works if you run it with stdin coming from the console on Linux. It does not work if you run it with stdin coming from the console on Windows.

It must be some sort of Windows console oddity.

markgz
  • 6,054
  • 1
  • 19
  • 41
0

In order to see the actual output, start the debugger with Ctrl-F5. This will keep the console window open.

See this answer for more information.

Community
  • 1
  • 1
Taylor Price
  • 622
  • 1
  • 8
  • 21
-2

Your loop is permanent. Meaning it doesn't end.. so it doesn't reach the call of printf.

EOF is indicator that clearly doesn't work like that here. You must break the loop on a specific key. Like enter for instance, which character decimal representation is 13 for carriage return. 10 for NL.

DeltaProxy
  • 208
  • 1
  • 2
  • 7