Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22260

printf() prints only the first character

$
0
0

Recently I came across an example from the book "The C Programming Language" and decided to run it:

#include <stdio.h>#define IN 1#define OUT 0int 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);}

There is a problem related to printf(). For some reason, only the first character is printed, although there should be 3 of them. What could be the problem?

System - Windows. For editing code I use Visual Studio Code. Compiler - gcc.

Interesting fact: on macOS this problem do not occur and the output is as needed.

Input:

a       ab abcabcd

Output:

4

Desired output:

4 4 14

Thanks in advance!

ANSWER:

I used control-C thinking that it was suitable as an EOF-indicator. The correct option is to use control-Z.


Viewing all articles
Browse latest Browse all 22260


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>