question

D

darklight

Just picked up on c programming again and doing
Kernighan & Ritchie c programming

the question is this why does the program below
not work;

#include <stdio.h>

/* count characters in input: 1st version */

main()
{
        long nc;

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

typed as in book

to get it to work i have to do this:

#include <stdio.h>

/* count characters in input: 1st version */

int main(void)
{
        long nc;

        nc = 0;

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


could the answer be that the programs in the above book
was wrote for the old compilers a detailed explanation
would be most helpful thanks for your help
#include <stdio.h>

/* count characters in input: 1st version */

int main(void)
{
        long nc;

        nc = 0;

        while(getchar() != EOF)
        {
          ++nc;
          printf("%ld\n",nc);
        }
        return 0;
}
owe should this be the out put to the above program:

user@linux-1669:~/cpro/ch1> ./count
user
1
2
3
4
5
 
L

Lowell Gilbert

darklight said:
the question is this why does the program below
not work;

I don't know. It looks and works okay as far as I'm concerned.

What do you think is wrong? I notice that some of your changes in
your modified version alter the behaviour of the program, which
originally only printed the length once, after all of the input had
been read.
 
A

Andrew Poelstra

Just picked up on c programming again and doing
Kernighan & Ritchie c programming

the question is this why does the program below
not work;

#include <stdio.h>

/* count characters in input: 1st version */

main()

Main returns int now. What version of K&R are you using? The 1st one isn't
all that useful for learning C. (Whether it's useful at all I don't know.)

int main (void)
{
        long nc;
        nc = 0;

These could be combined into
long nc = 0;
just so you know.
       
        while(getchar() != EOF)
          ++nc;
         printf("%ld\n",nc);

Since main returns an int, you should have a
return 0;
here.
}

typed as in book

to get it to work i have to do this:

#include <stdio.h>

/* count characters in input: 1st version */

int main(void)
{
        long nc;

        nc = 0;

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

There! See, you fixed it all.
could the answer be that the programs in the above book
was wrote for the old compilers a detailed explanation
would be most helpful thanks for your help

Now even an old compiler; the original K&R was written even
before the first /standard/ of C. Hence, it has a lot of
weird conventions that aren't valid today. (Also, the 2nd
version's example listings have been changed in unrelated
ways to be clearer, cleaner, or conciser.)
 
A

Ancient_Hacker

darklight said:
Just picked up on c programming again and doing
Kernighan & Ritchie c programming

the question is this why does the program below
not work;

Depends what you mean by "work".

Different implementations of the C I/O library will buffer the input
and output in differnt ways, so your input and output may not appear at
the expected times.
 
O

osmium

Andrew Poelstra said:
Main returns int now. What version of K&R are you using? The 1st one isn't
all that useful for learning C. (Whether it's useful at all I don't know.)

That's a direct copy from K&R ANSI C on p 59. If you care to get a copy so
you can actually look at it, instead of speculating on what it might say, or
what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
same text is available with other ISBNs as well.
 
C

Coos Haak

Op Wed, 16 Aug 2006 08:17:34 -0700 schreef osmium:
That's a direct copy from K&R ANSI C on p 59. If you care to get a copy so
you can actually look at it, instead of speculating on what it might say, or
what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
same text is available with other ISBNs as well.

It's a direct copy of page 18 in the second edition.
ISB number 0-13-110362-8 or 0-13-110370-9
Which version do you have?
 
O

osmium

Coos Haak said:
Op Wed, 16 Aug 2006 08:17:34 -0700 schreef osmium:


It's a direct copy of page 18 in the second edition.
ISB number 0-13-110362-8 or 0-13-110370-9
Which version do you have?

I have neither, I have the one I described in my post. Polestra, was
harping on the implicit int return type, what followed that was immaterial.
I just opened K&R to a random page, and simply mentioned that page as an
example. Disregarding bindings, natural language used, and so on, there are
only two K&Rs. The book I described is as good as it gets, ANSI C. All
good pedants should have a copy at their fingertips.
 
M

Martin Ambuhl

Andrew said:
Main returns int now. What version of K&R are you using?

In the version given, all versions of C before C99 had main returning an
int. This is not a question of whether "Main returns int now." The
"now" is bogus. With versions of C before C99, implicit int was
supported and
main() { /* ... */ }
has exactly the same return type as
int main() { /* ... */ }
The 1st one isn't
all that useful for learning C.

It was certainly good enough for me to learn C from in 1978. Many other
people also learned C from K&R in that era. has the intelligence of
would-be programmers dropped that much in the last 28 years?
 
A

Andrew Poelstra

That's a direct copy from K&R ANSI C on p 59. If you care to get a copy so
you can actually look at it, instead of speculating on what it might say, or
what you think it *should* say, it is ISBN 0-13-220362-8. I presume the
same text is available with other ISBNs as well.

I'm not speculating; I merely asked a question. Had I said something like
"I think you're using K&R1" or "Why are you using K&R1?" it would have been
speculating.

According to the eBay guy who sold me K&R1 with the guise of being the 2nd
version, both books have the same ISBN, and therefore the same default
display picture.
 
A

Andrew Poelstra

I have neither, I have the one I described in my post. Polestra, was
harping on the implicit int return type, what followed that was immaterial.
I just opened K&R to a random page, and simply mentioned that page as an
example. Disregarding bindings, natural language used, and so on, there are
only two K&Rs. The book I described is as good as it gets, ANSI C. All
good pedants should have a copy at their fingertips.

All I have is K&R1. I did try to get the second version, but due to false
advertising (which in turn was due to both K&R's *apparently* having the
same ISBN), I recieved the first. $20 I'll never get back.


What I posted was not immaterial, nor was it speculation. It was an honest
question.

I am not a pedant, nor am I trying to be, because I'm clearly not yet
qualified to be one. :)
 
A

Andrew Poelstra

In the version given, all versions of C before C99 had main returning an
int. This is not a question of whether "Main returns int now." The
"now" is bogus. With versions of C before C99, implicit int was
supported and
main() { /* ... */ }
has exactly the same return type as
int main() { /* ... */ }


It was certainly good enough for me to learn C from in 1978. Many other
people also learned C from K&R in that era. has the intelligence of
would-be programmers dropped that much in the last 28 years?

Mine hasn't (although my ownership of books is less than most people), but
schools now teach basically nothing but Java, and I read through the
curriculum of a high school programming course: they teach VB up to and
including array syntax. Having been overqualified for said course from
about a week after starting programming, I didn't take it.

So, yeah, 20 years ago you had to be a /lot/ smarter to be considered a
programmer. (With .NET I hear that you don't even need to touch a line
of code to "program"!)
 
K

Keith Thompson

Andrew Poelstra said:
Main returns int now. What version of K&R are you using? The 1st one isn't
all that useful for learning C. (Whether it's useful at all I don't know.)

Implicit int wasn't banned until C99. K&R2 only covers C89/C90.

"int main(void)" is better than "main()", but either one should be
accepted by any compiler that's not being used in strict C99 mode.

[...]
Since main returns an int, you should have a
return 0;
here.

Yes, but it's not actually required in any version of C.

If I recall correctly, most of the examples in K&R1 don't bother
returning an explicit value from main().

In C90, falling off the end of main() returns an unspecified status to
the calling environment, but it doesn't invoke undefined behavior.

In C99, falling off the end of main() does an implicit "return 0;".

Adding the "return 0;" is certainly a good idea, but it's not likely to
be the cause of the OP's problem.
 
D

Default User

Andrew Poelstra wrote:

According to the eBay guy who sold me K&R1 with the guise of being
the 2nd version, both books have the same ISBN, and therefore the
same default display picture.

My research indicates that the ISBN for the first edition was
0131101633:

The C programming language
Brian W. Kernighan, Dennis M. Ritchie
Publisher: Englewood Cliffs, N.J. : Prentice-Hall, c1978.
ISBN: 0131101633 DDC: 1.6424 LCC: QA76.73
http://isbndb.com/d/book/the_c_programming_language.html



The second was 0131103628:

The C programming language
Brian W. Kernighan, Dennis M. Ritchie
Publisher: Englewood Cliffs, N.J. : Prentice Hall, c1988.
ISBN: 0131103628 DDC: 5.133 LCC: QA76.73 Edition: (pbk.)
http://isbndb.com/d/book/the_c_programming_language_a05.html





Brian
 
K

Keith Thompson

darklight said:
Just picked up on c programming again and doing
Kernighan & Ritchie c programming

the question is this why does the program below
not work;

No, the question is *how* does the program below not work.
#include <stdio.h>

/* count characters in input: 1st version */

main()
{
        long nc;

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

typed as in book

So what *exactly* happened when you tried it?

I can compile and execute it without error. It has some archaic
constructs, but any compiler not being used in strict C99 mode should
accept it, perhaps with a warning or two.

One thing to watch out for is that input and output can be buffered.
The program doesn't produce any output until it reaches end-of-file.
If you're running it with input from the keyboard, that means you have
to type some system-specific key sequence to denote end-of-file. (On
Unix-like systems, type control-D at the beginning of a line, or
control-D twice in the middle of a line. In Windows or MS-DOS, use
control-Z.)

The subject header of your message, "question", doesn't give us any
clue what you're asking about. The subject should briefly describe
the problem; if it doesn't, a lot of people are going to skip over it
without reading it.

If something doesn't work, you need to tell us exactly *how* it
doesn't work. Does it fail to compile? Do you get any error
messages? If so, *exactly* what are they?

I recommend reading <http://www.catb.org/~esr/faqs/smart-questions.html>.
 
D

darklight

Andrew said:
I'm not speculating; I merely asked a question. Had I said something like
"I think you're using K&R1" or "Why are you using K&R1?" it would have
been speculating.

According to the eBay guy who sold me K&R1 with the guise of being the 2nd
version, both books have the same ISBN, and therefore the same default
display picture.
using second edition if that makes any difference
 
C

Coos Haak

Op Thu, 17 Aug 2006 10:03:40 +0100 schreef darklight:
using second edition if that makes any difference

As might conclude from these postings, there is no difference in the first
and second edition in the program you showed us ;-(
 
D

darklight

Keith Thompson wrote: (On
Unix-like systems, type control-D thanks for that
now i am getting the correct out put:

IE once program starts,

#include<stdio.h>

/* count characters in input: 1st version */

int main(void)
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%ld\n",nc);
return 0;
}




and i enter say glen and press
control + d the out put i just five

with this program:

#include<stdio.h>

/* count characters in input: 1st version */

int main(void)
{
long nc;

nc = 0;
while(getchar() != EOF)
{
++nc;
printf("%ld\n",nc);
}
return 0;
}
and type user then press enter i get

user@linux-1669:~/cpro> ./a.out
user
1
2
3
4
5

thanks for your help!!
 
P

Philip Potter

while(getchar() != EOF)
++nc;
printf("%ld\n",nc);
<snip>
and i enter say glen and press
control + d the out put i just five

How many times is printf() called here?
while(getchar() != EOF)
{
++nc;
printf("%ld\n",nc);
}

And how many times here?

Those curly brackets are having an affect on the execution, despite the fact
that the printf() call is at the "outer" indent level. (Indentation is
ignored by the compiler).
and type user then press enter i get

user@linux-1669:~/cpro> ./a.out
user
1
2
3
4
5

Philip
 
C

Chen Shusheng

I guess the book is correct. Just count the word before "EOF" comning.
But that code is not so obviously. So I change a little. See below:
-------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* count characters in input: 1st version */
main()
{
long nc;
nc = 0;
while(getche() != 'e')
++nc;
printf("\n%ld\n",nc);
system("pause");
return 0;
}
-------------------------------
 
K

Keith Thompson

Chen Shusheng said:
I guess the book is correct. Just count the word before "EOF" comning.
But that code is not so obviously. So I change a little. See below:
-------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* count characters in input: 1st version */
main()
{
long nc;
nc = 0;
while(getche() != 'e')
++nc;
printf("\n%ld\n",nc);
system("pause");
return 0;
}

Please don't top-post. Read <http://www.caliburn.nl/topposting.html>.

Your program is full of system-specific features that will not work on
many systems, and that aren't at all necessary.

The header <conio.h>, the getche() fucntion, and the external "pause"
command are all system-specific, and you don't need to use any of
them.

Your loop continues until getche() returns 'e'. Presumably this means
that the only way for a user to terminate the program is to type 'e';
if there's an EOF condition, the program will probably go into an
infinite loop. This is not very useful.

Rather than "main()", you should use "int main(void)".

You have no indentation. This makes your code very difficult to read.
Possibly you indented with tabs rather than spaces, and some news
software somewhere deleted them. Use only spaces for indentation, at
least for code you post here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problems with reading binary data files 4
beginners problem 10
Remove repeated words from a file 3
constructing robust constructors 15
ctime_r() warnings 3
What's wrong ? 21
[ANN] Rassmalog 12.0.0 0
Parsing a string 44

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top