[sclug] Off topic C question
Alex Butcher
lug at assursys.co.uk
Fri Sep 22 10:49:29 UTC 2006
On Fri, 22 Sep 2006, Patrick wrote:
> Hi all,
>
> Can anyone see why this character counting program always finds zero
> characters? Its based on an example from K&R Chapter 1 which also fails.
Using gcc -ansi -pedantic helps debug this sort of thing. Oh, and I do hope
you're using the ANSI edition of K&R! :-)
Taking the problems it finds step-by-step...
>
> #include "stdio.h"
>
> int main()
> {
> /* count characters*/
> FILE *inFile;
> inFile = fopen("motd.xml", "r");
^ move this line below the next two declarations
> int c = 0;
> long lc = 0;
>
> while ((c = getc(inFile)) != EOF)
> {
> if (c == "l") {
You're comparing an integer (c) with a pointer to a char here. As Dickon
pointed out, you want single quotes here, so the character is a constant. I
suspect that's also supposed to be '\n' (newline) rather than anything else.
> ++lc;
> }
>
>
> }
>
> printf("Typed %1d lines\n", nc);
lc, rather than nc, as Simon pointed out.
> system("pause");
>
> return 0;
>
> }
Best Regards,
Alex.
--
Alex Butcher, Bristol UK. PGP/GnuPG ID:0x5010dbff
"[T]he whole point about the reason why I think it is important we go for
identity cards and an identity database today is that identity fraud and
abuse is a major, major problem. Now the civil liberties aspect of it, look
it is a view, I don't personally think it matters very much."
- Tony Blair, 6 June 2006 <http://www.number-10.gov.uk/output/Page9566.asp>
More information about the Sclug
mailing list