Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

win-nt from the people who invented edlin. -- MaDsen Wikholm, mwikholm@at8.abo.fi


devel / comp.lang.c / Character counting program not working as expected.

SubjectAuthor
* Character counting program not working as expected.Vasu Garg
+- Re: Character counting program not working as expected.Mark Bluemel
+* Re: Character counting program not working as expected.Branimir Maksimovic
|`- Re: Character counting program not working as expected.James Kuyper
+* Re: Character counting program not working as expected.Malcolm McLean
|+* Re: Character counting program not working as expected.Lew Pitcher
||`- Re: Character counting program not working as expected.Lew Pitcher
|`- Re: Character counting program not working as expected.Keith Thompson
`- Re: Character counting program not working as expected.Bart

1
Character counting program not working as expected.

<9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18638&group=comp.lang.c#18638

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1014:: with SMTP id d20mr13621477qte.152.1633940908360; Mon, 11 Oct 2021 01:28:28 -0700 (PDT)
X-Received: by 2002:a05:620a:1a89:: with SMTP id bl9mr3899286qkb.459.1633940908001; Mon, 11 Oct 2021 01:28:28 -0700 (PDT)
Path: rocksolid2!i2pn.org!usenet.goja.nl.eu.org!2.eu.feeder.erje.net!feeder.erje.net!news.uzoreto.com!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 11 Oct 2021 01:28:27 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=2409:4070:4e8a:b721:2ce6:907a:bc91:2dbf; posting-account=sJuAxAoAAABFdiLN9r46oED6V__A1g5S
NNTP-Posting-Host: 2409:4070:4e8a:b721:2ce6:907a:bc91:2dbf
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
Subject: Character counting program not working as expected.
From: vasuvga...@gmail.com (Vasu Garg)
Injection-Date: Mon, 11 Oct 2021 08:28:28 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 21
 by: Vasu Garg - Mon, 11 Oct 2021 08:28 UTC

I am learning the C programming language as my 1st from 'THE C PROGRAMMING LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan and Dennis M. Ritchie.

In section 1.5.2, the following code is given (nc as variable instead of counter) which I am trying to run, however the program does not give me the desired output which would be number of characters entered, infact it gives me no output.

#include <stdio.h>

main()
{ long counter;

counter = 0;

while (getchar() != EOF)
++counter;

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

I modified the program to also print as many times the text "test" as the while loop runs by adding a printf("test\n"); before ++counter.
This time upon entering input it prints test the right number of times but still does not print the value stored in counter afterward .

What might the problem be?

Re: Character counting program not working as expected.

<5e42079c-c72f-4e23-8787-ff28a139ad48n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18639&group=comp.lang.c#18639

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a17:902:64d6:b0:13e:a59e:332c with SMTP id y22-20020a17090264d600b0013ea59e332cmr22992112pli.30.1633942013906;
Mon, 11 Oct 2021 01:46:53 -0700 (PDT)
X-Received: by 2002:ae9:d845:: with SMTP id u66mr7128920qkf.199.1633942013634;
Mon, 11 Oct 2021 01:46:53 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 11 Oct 2021 01:46:53 -0700 (PDT)
In-Reply-To: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23c7:a280:3401:2006:95ac:e2fc:534c;
posting-account=3LA7mQoAAAByiBtHIUvpFq0_QEKnHGc9
NNTP-Posting-Host: 2a00:23c7:a280:3401:2006:95ac:e2fc:534c
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5e42079c-c72f-4e23-8787-ff28a139ad48n@googlegroups.com>
Subject: Re: Character counting program not working as expected.
From: mark.blu...@gmail.com (Mark Bluemel)
Injection-Date: Mon, 11 Oct 2021 08:46:53 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 28
 by: Mark Bluemel - Mon, 11 Oct 2021 08:46 UTC

On Monday, 11 October 2021 at 09:28:34 UTC+1, vasuv...@gmail.com wrote:
> I am learning the C programming language as my 1st from 'THE C PROGRAMMING LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan and Dennis M. Ritchie.
>
> In section 1.5.2, the following code is given (nc as variable instead of counter) which I am trying to run, however the program does not give me the desired output which would be number of characters entered, infact it gives me no output.
>
> #include <stdio.h>
>
> main()
> {
> long counter;
>
> counter = 0;
>
> while (getchar() != EOF)
> ++counter;
>
> printf("%ld\n", counter);
> }
>
> I modified the program to also print as many times the text "test" as the while loop runs by adding a printf("test\n"); before ++counter.
> This time upon entering input it prints test the right number of times but still does not print the value stored in counter afterward .
>
> What might the problem be?

Do you know how to end input on your particular platform? If you just type some text and stop, the getchar() will wait indefinitely for the next input character, as it won't receive an EOF.

Commonly, ctrl-D will make the end of interactive input.

If you redirect input to your program from a file, then it would get an EOF, so I expect you'd find the expected behaviour.

Re: Character counting program not working as expected.

<0iT8J.185288$o45.147739@fx46.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18640&group=comp.lang.c#18640

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!i2pn.org!weretis.net!feeder8.news.weretis.net!news.roellig-ltd.de!open-news-network.org!peer03.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.iad.POSTED!not-for-mail
Newsgroups: comp.lang.c
From: branimir...@icloud.com (Branimir Maksimovic)
Subject: Re: Character counting program not working as expected.
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
User-Agent: slrn/1.0.3 (Darwin)
Lines: 37
Message-ID: <0iT8J.185288$o45.147739@fx46.iad>
X-Complaints-To: abuse@usenet-news.net
NNTP-Posting-Date: Mon, 11 Oct 2021 09:16:44 UTC
Organization: usenet-news.net
Date: Mon, 11 Oct 2021 09:16:44 GMT
X-Received-Bytes: 1716
 by: Branimir Maksimovic - Mon, 11 Oct 2021 09:16 UTC

On 2021-10-11, Vasu Garg <vasuvgarge@gmail.com> wrote:
> I am learning the C programming language as my 1st from 'THE C PROGRAMMING
> LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan and Dennis M.
> Ritchie.
>
> In section 1.5.2, the following code is given (nc as variable instead of
> counter) which I am trying to run, however the program does not give me the
> desired output which would be number of characters entered, infact it gives
> me no output.
>
> #include <stdio.h>
>
> main() { long counter;
>
> counter = 0;
>
> while (getchar() != EOF)
{ puts("test");
++counter;
} >
> printf("%ld\n", counter); }
>
> I modified the program to also print as many times the text "test" as the
> while loop runs by adding a printf("test\n"); before ++counter. This time
> upon entering input it prints test the right number of times but still does
> not print the value stored in counter afterward .
>
> What might the problem be?
Braces :P

--

7-77-777
Evil Sinner!
with software, you repeat same experiment, expecting different results...

Re: Character counting program not working as expected.

<ef55fa62-d9df-4656-ac39-15b1b2790174n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18641&group=comp.lang.c#18641

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:9202:: with SMTP id u2mr13949624qkd.454.1633944421829;
Mon, 11 Oct 2021 02:27:01 -0700 (PDT)
X-Received: by 2002:ac8:7e86:: with SMTP id w6mr13083977qtj.277.1633944421700;
Mon, 11 Oct 2021 02:27:01 -0700 (PDT)
Path: rocksolid2!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 11 Oct 2021 02:27:01 -0700 (PDT)
In-Reply-To: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:65e9:9a1:f921:2520;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:65e9:9a1:f921:2520
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ef55fa62-d9df-4656-ac39-15b1b2790174n@googlegroups.com>
Subject: Re: Character counting program not working as expected.
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Mon, 11 Oct 2021 09:27:01 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 29
 by: Malcolm McLean - Mon, 11 Oct 2021 09:27 UTC

On Monday, 11 October 2021 at 09:28:34 UTC+1, vasuv...@gmail.com wrote:
> I am learning the C programming language as my 1st from 'THE C PROGRAMMING LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan and Dennis M. Ritchie.
>
> In section 1.5.2, the following code is given (nc as variable instead of counter) which I am trying to run, however the program does not give me the desired output which would be number of characters entered, infact it gives me no output.
>
> #include <stdio.h>
>
> main()
> {
> long counter;
>
> counter = 0;
>
> while (getchar() != EOF)
> ++counter;
>
> printf("%ld\n", counter);
> }
>
> I modified the program to also print as many times the text "test" as the while loop runs by adding a printf("test\n"); before ++counter.
> This time upon entering input it prints test the right number of times but still does not print the value stored in counter afterward .
>
> What might the problem be?
>
As Mark said, you are never generating the EOF. control-D or control-Z are the normal ways of doing this,
depending on your platform.
However EOF in interactive input is a bit of a hangover from the old days of teletypes.

It might be easier just to use newline ('\n') to indicate end of input. The way standard input works is that the user
composes a line on the termial, then the terminal sends it as a whole line to the program - it's line-oriented.

Re: Character counting program not working as expected.

<sk11fp$ef5$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18642&group=comp.lang.c#18642

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Character counting program not working as expected.
Date: Mon, 11 Oct 2021 10:51:33 +0100
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <sk11fp$ef5$1@dont-email.me>
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 11 Oct 2021 09:51:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="22a96d743727ed2d09e86d7f250e4e6a";
logging-data="14821"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2u4KfyGL10mWFVAmILEJO40QKFhetpic="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:HeKjaapIUc9OJ0xQRzJkodpiGjM=
In-Reply-To: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 211010-4, 10/10/2021), Outbound message
 by: Bart - Mon, 11 Oct 2021 09:51 UTC

On 11/10/2021 09:28, Vasu Garg wrote:
> I am learning the C programming language as my 1st from 'THE C PROGRAMMING LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan and Dennis M. Ritchie.
>
> In section 1.5.2, the following code is given (nc as variable instead of counter) which I am trying to run, however the program does not give me the desired output which would be number of characters entered, infact it gives me no output.
>
> #include <stdio.h>
>
> main()
> {
> long counter;
>
> counter = 0;
>
> while (getchar() != EOF)
> ++counter;
>
> printf("%ld\n", counter);
> }
>
> I modified the program to also print as many times the text "test" as the while loop runs by adding a printf("test\n"); before ++counter.
> This time upon entering input it prints test the right number of times but still does not print the value stored in counter afterward .
>
> What might the problem be?
>

Try:

long counter;
int ch;

counter = 0;

while ((ch=getchar()) != EOF) {
printf("%d\n", ch);
++counter;
}

On Windows, it waits for a line of input (press Enter) until printing
the characters on the line, the waits for another.

To end it, I have to type Ctlt-Z followed by Enter, on its own line.

Alternately, run it with input from a file:

prog <input

When it works, remove the ch code.

Re: Character counting program not working as expected.

<sk1dhd$ecv$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18643&group=comp.lang.c#18643

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: Character counting program not working as expected.
Date: Mon, 11 Oct 2021 13:17:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <sk1dhd$ecv$1@dont-email.me>
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
<ef55fa62-d9df-4656-ac39-15b1b2790174n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 11 Oct 2021 13:17:33 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ff0ac6fe84f7d4d0ca00ad5c81b5c11d";
logging-data="14751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fCeew6stvTxn1jhORTaD5EkqIdDqXtTU="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:uPr4Mx2WJrUJ/9Mij/80HtJs1xo=
 by: Lew Pitcher - Mon, 11 Oct 2021 13:17 UTC

On Mon, 11 Oct 2021 02:27:01 -0700, Malcolm McLean wrote:

> On Monday, 11 October 2021 at 09:28:34 UTC+1, vasuv...@gmail.com wrote:
>> I am learning the C programming language as my 1st from 'THE C
>> PROGRAMMING LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan
>> and Dennis M. Ritchie.
>>
>> In section 1.5.2, the following code is given (nc as variable instead
>> of counter) which I am trying to run, however the program does not give
>> me the desired output which would be number of characters entered,
>> infact it gives me no output.
>>
>> #include <stdio.h>
>>
>> main()
>> {
>> long counter;
>>
>> counter = 0;
>>
>> while (getchar() != EOF)
>> ++counter;
>>
>> printf("%ld\n", counter);
>> }
>>
>> I modified the program to also print as many times the text "test" as
>> the while loop runs by adding a printf("test\n"); before ++counter.
>> This time upon entering input it prints test the right number of times
>> but still does not print the value stored in counter afterward .
>>
>> What might the problem be?
>>
> As Mark said, you are never generating the EOF. control-D or control-Z
> are the normal ways of doing this,
> depending on your platform.
> However EOF in interactive input is a bit of a hangover from the old
> days of teletypes.
>
> It might be easier just to use newline ('\n') to indicate end of input.
> The way standard input works is that the user composes a line on the
> termial, then the terminal sends it as a whole line to the program -
> it's line-oriented.

The beauty of the K&R example program is that it forms one of the
tools in the student's learning of I/O redirection and pipes.

While the OP may find it convenient to take your advice and terminate
input at the first newline, this will only produce your expected results
from this program when run with input from a terminal.

It fails when input comes from a file. It especially fails on Unix when
input comes from a binary file (Unix makes no distinction between "text"
and "binary", so fopen(...,"r") is the same as fopen(...,"rb") ), in that
there are often more opportunities for the binary value that represents a
newline to occur in a "binary" file than in a "text" file.

To the OP, your problem, as stated by others here, is likely that you do
not introduce an End_Of_File (EOF) condition into the input of your
program, and thus, never terminate your counting loop.

Find out how you signal EOF on your platform, and try your program again
with that. On Unix (and Unix-ish systems like Linux), you usually type
control-D to signal End_Of_File to terminal input, while on a Microsoft
Windows platform, you type control-Z.

HTH
--
Lew Pitcher
"In Skills, We Trust"

Re: Character counting program not working as expected.

<sk1el6$ecv$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18644&group=comp.lang.c#18644

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: Character counting program not working as expected.
Date: Mon, 11 Oct 2021 13:36:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 134
Message-ID: <sk1el6$ecv$2@dont-email.me>
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
<ef55fa62-d9df-4656-ac39-15b1b2790174n@googlegroups.com>
<sk1dhd$ecv$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 11 Oct 2021 13:36:38 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ff0ac6fe84f7d4d0ca00ad5c81b5c11d";
logging-data="14751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185c88pxcKnMcA3j5ysUUM4MS6d5k75shM="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:bSL+3yzd1r0rtN2+kU94OOBQAv8=
 by: Lew Pitcher - Mon, 11 Oct 2021 13:36 UTC

On Mon, 11 Oct 2021 13:17:33 +0000, Lew Pitcher wrote:

> On Mon, 11 Oct 2021 02:27:01 -0700, Malcolm McLean wrote:
>
>> On Monday, 11 October 2021 at 09:28:34 UTC+1, vasuv...@gmail.com wrote:
>>> I am learning the C programming language as my 1st from 'THE C
>>> PROGRAMMING LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan
>>> and Dennis M. Ritchie.
>>>
>>> In section 1.5.2, the following code is given (nc as variable instead
>>> of counter) which I am trying to run, however the program does not
>>> give me the desired output which would be number of characters
>>> entered, infact it gives me no output.
>>>
>>> #include <stdio.h>
>>>
>>> main()
>>> {
>>> long counter;
>>>
>>> counter = 0;
>>>
>>> while (getchar() != EOF)
>>> ++counter;
>>>
>>> printf("%ld\n", counter);
>>> }
>>>
>>> I modified the program to also print as many times the text "test" as
>>> the while loop runs by adding a printf("test\n"); before ++counter.
>>> This time upon entering input it prints test the right number of times
>>> but still does not print the value stored in counter afterward .
>>>
>>> What might the problem be?
>>>
>> As Mark said, you are never generating the EOF. control-D or control-Z
>> are the normal ways of doing this,
>> depending on your platform.
>> However EOF in interactive input is a bit of a hangover from the old
>> days of teletypes.
>>
>> It might be easier just to use newline ('\n') to indicate end of input.
>> The way standard input works is that the user composes a line on the
>> termial, then the terminal sends it as a whole line to the program -
>> it's line-oriented.
>
> The beauty of the K&R example program is that it forms one of the tools
> in the student's learning of I/O redirection and pipes.
>
> While the OP may find it convenient to take your advice and terminate
> input at the first newline, this will only produce your expected results
> from this program when run with input from a terminal.
>
> It fails when input comes from a file. It especially fails on Unix when
> input comes from a binary file (Unix makes no distinction between "text"
> and "binary", so fopen(...,"r") is the same as fopen(...,"rb") ), in
> that there are often more opportunities for the binary value that
> represents a newline to occur in a "binary" file than in a "text" file.
>
> To the OP, your problem, as stated by others here, is likely that you do
> not introduce an End_Of_File (EOF) condition into the input of your
> program, and thus, never terminate your counting loop.
>
> Find out how you signal EOF on your platform, and try your program again
> with that. On Unix (and Unix-ish systems like Linux), you usually type
> control-D to signal End_Of_File to terminal input, while on a Microsoft
> Windows platform, you type control-Z.
>
> HTH

The OP's character counting program:
09:28 $ nl -ba charcount.c
1 #include <stdio.h>
2
3 main()
4 {
5 long counter;
6
7 counter = 0;
8
9 while (getchar() != EOF)
10 ++counter;
11
12 printf("%ld\n", counter);
13 }

Mr. McLean's suggested change:
09:28 $ nl -ba charcount_mm.c
1 #include <stdio.h>
2
3 main()
4 {
5 long counter;
6
7 counter = 0;
8
9 while (getchar() != '\n')
10 ++counter;
11
12 printf("%ld\n", counter);
13 }

line, word and character counts of OP's program, standard utility
09:28 $ wc charcount.c
13 17 134 charcount.c

line, word and character counts of McLean's change, standard utility
09:28 $ wc charcount_mm.c
13 17 135 charcount_mm.c

OP's character count output, it's own source code as input
09:29 $ charcount <charcount.c
134
Note that this agrees with the character count from the standard utility

OP's character count output, McLean's changed source code as input
09:29 $ charcount <charcount_mm.c
135
Note that this agrees with the character count from the standard utility

Output from McLean's suggested change, input is OP's source code
09:29 $ charcount_mm <charcount.c
18
Oops. Incorrect count

Output from McLean's suggested change, input is source with McLean's
suggested change
09:29 $ charcount_mm <charcount_mm.c
18
Oops. Incorrect count

--
Lew Pitcher
"In Skills, We Trust"

Re: Character counting program not working as expected.

<87k0ij62p7.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18645&group=comp.lang.c#18645

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Character counting program not working as expected.
Date: Mon, 11 Oct 2021 07:44:36 -0700
Organization: None to speak of
Lines: 56
Message-ID: <87k0ij62p7.fsf@nosuchdomain.example.com>
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
<ef55fa62-d9df-4656-ac39-15b1b2790174n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="d1524f439902674a2af25710c0ea0744";
logging-data="28806"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YAneqg7pMEvbqbTB/LvNV"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:IQT8cyYPOEVV6hQZj8SJW2EAcng=
sha1:l0dCit4SE+Gm4WLsu4J8nL3i/2g=
 by: Keith Thompson - Mon, 11 Oct 2021 14:44 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
> On Monday, 11 October 2021 at 09:28:34 UTC+1, vasuv...@gmail.com wrote:
>> I am learning the C programming language as my 1st from 'THE C
>> PROGRAMMING LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan
>> and Dennis M. Ritchie.
>>
>> In section 1.5.2, the following code is given (nc as variable instead
>> of counter) which I am trying to run, however the program does not
>> give me the desired output which would be number of characters
>> entered, infact it gives me no output.
>>
>> #include <stdio.h>
>>
>> main()
>> {
>> long counter;
>>
>> counter = 0;
>>
>> while (getchar() != EOF)
>> ++counter;
>>
>> printf("%ld\n", counter);
>> }
>>
>> I modified the program to also print as many times the text "test" as
>> the while loop runs by adding a printf("test\n"); before ++counter.
>> This time upon entering input it prints test the right number of
>> times but still does not print the value stored in counter afterward
>> .
>>
>> What might the problem be?
>>
> As Mark said, you are never generating the EOF. control-D or control-Z
> are the normal ways of doing this, depending on your platform.
> However EOF in interactive input is a bit of a hangover from the old
> days of teletypes.
>
> It might be easier just to use newline ('\n') to indicate end of
> input. The way standard input works is that the user composes a line
> on the termial, then the terminal sends it as a whole line to the
> program - it's line-oriented.

That solves a different problem. If you're having difficulty with
something while learning a language, giving up and solving a simpler
problem instead is not a good way to make progress.

Also, if you use `while (getchar() != '\n')`, you'll have an
infinite loop if you reach end-of-file before seeing a newline.
If you want to count the characters in a single line, you need to
check for '\n' *and* EOF.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: Character counting program not working as expected.

<sk1pfj$5e3$5@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18648&group=comp.lang.c#18648

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Character counting program not working as expected.
Date: Mon, 11 Oct 2021 12:41:23 -0400
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <sk1pfj$5e3$5@dont-email.me>
References: <9d875c89-0736-4501-b024-d69e78a2b8een@googlegroups.com>
<0iT8J.185288$o45.147739@fx46.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 11 Oct 2021 16:41:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a7992d9703e22c1e39fde38bf0a2b887";
logging-data="5571"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GKo9ahWQpL3eAEp++wpZVMIIkbyL9zXo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Cancel-Lock: sha1:GNkV0w98QUeSsuwORPeNeB/mqnc=
In-Reply-To: <0iT8J.185288$o45.147739@fx46.iad>
Content-Language: en-US
 by: James Kuyper - Mon, 11 Oct 2021 16:41 UTC

On 10/11/21 5:16 AM, Branimir Maksimovic wrote:
> On 2021-10-11, Vasu Garg <vasuvgarge@gmail.com> wrote:
>> I am learning the C programming language as my 1st from 'THE C PROGRAMMING
>> LANGUAGE (ANSI C Version), 2nd Ed.' by Brian W. Kernighan and Dennis M.
>> Ritchie.
>>
>> In section 1.5.2, the following code is given (nc as variable instead of
>> counter) which I am trying to run, however the program does not give me the
>> desired output which would be number of characters entered, infact it gives
>> me no output.
>>
>> #include <stdio.h>
>>
>> main() { long counter;
>>
>> counter = 0;
>>
>> while (getchar() != EOF)
> {
> puts("test");
> ++counter;
> }
>>
>> printf("%ld\n", counter); }
>>
>> I modified the program to also print as many times the text "test" as the
>> while loop runs by adding a printf("test\n"); before ++counter. This time
>> upon entering input it prints test the right number of times but still does
>> not print the value stored in counter afterward .
>>
>> What might the problem be?
> Braces :P

His original program didn't need braces. The modified version he
described does need braces, as you indicated, but you have no evidence
to justify assuming that he left them out. With or without braces,
either version of the program should have executed the final printf()
call unless the loop failed to terminate. Ignoring that issue, both
versions of his code should have behavior that matches his description,
whether or not he put in those braces.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor