473,486 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I'm very confused(learning C)

Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on
chapter 1.5.1 and here's the program you're sposed to make:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
int c;

c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}

Ok, now here's what I'm confused about: I read it all and everything
and I'm not sure what it's sposed to do. I tried it and say if I type:

a

Then it'll mimic it, so if I type "a" and press enter it'll do this:

a
a

Is that what it's supposed to do? And if so then why doesn't it make a
difference if I take out the "!"(not equal to(I think)).

Any help is greatly appreciated, thanks!

Nov 14 '05 #1
6 1805


m_a_t_t wrote:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on
chapter 1.5.1 and here's the program you're sposed to make:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
int c;

c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}

Ok, now here's what I'm confused about: I read it all and everything
and I'm not sure what it's sposed to do. I tried it and say if I type:

a

Then it'll mimic it, so if I type "a" and press enter it'll do this:

a
a

Is that what it's supposed to do? And if so then why doesn't it make a
difference if I take out the "!"(not equal to(I think)).

Any help is greatly appreciated, thanks!


You're confused because the echo of your input
and the output of the program are showing up mingled
together on the same screen, and it's hard to tell
them apart. Try taking the input from some other
source and/or sending the output to a different
destination. On Unix you'd do something like

myprogram <inputfile >outputfile

to copy the contents of "inputfile" to "outputfile," or

myprogram <inputfile

to copy "inputfile" to the screen (more correctly,
"to the standard output").

You may also encounter trouble in generating the
"end of file" indication from the keyboard; different
systems have different conventions. On most Unixes
you enter the CTRL-D key combination at the start of
a line to say you're through (this is typical, although
it can be changed), and on Windows you use CTRL-Z.

By the way, there are a few infelicities in the code
itself. `main()' should be `int main()' or better yet
`int main(void)', and there should be a `return 0;' just
before the final closing brace. (Ignore any pedants who
tell you that you need one or the other of these but not
both, depending on which version of the C Standard you're
using: such people are right, but it's a bad idea to be
guided by hair-splittin' nit-pickin' Philadelphia lawyers.)

--
Er*********@sun.com

Nov 14 '05 #2

m_a_t_t wrote:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on
chapter 1.5.1 and here's the program you're sposed to make:
Excellent choice of books, IMHO.
#include <stdio.h>

/* copy input to output; 1st version */
main()
{
int c;

c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}

Ok, now here's what I'm confused about: I read it all and everything
and I'm not sure what it's sposed to do. I tried it and say if I type:
a

Then it'll mimic it, so if I type "a" and press enter it'll do this:

a
a

Is that what it's supposed to do?
The first 'a' is yours, and comes from the keyboard. The second 'a' is
printed by the program using putchar. This is exactly what the program
is supposed to do.
And if so then why doesn't it make a
difference if I take out the "!"(not equal to(I think)).


If you take out the '!' (leaving just =, and not ==) then the while
loop will simply save EOF to the char every time you press the
keyboard.

Notice that c is declared as int, and not char. This is because the
operating system sends what is called and End Of File marker (EOF) as
the last piece of data when the 'file' has been completely read. This
marker cannot be a char because it has to be distinguishable from all
possible char data contained in the file. In this case the 'file' is
standard input, and comes from the keyboard. To simulate the EOF
character you need to use some sort of a special character sequence. On
Unix (and Unix type systems, like Linux) this the the CONTROL-D
sequence. On Windows it is CONTROL-Z.

Good luck learning C. [ brings back memories... ahhh. ]

-Jason

Nov 14 '05 #3
"m_a_t_t" <no******@gmail.com> writes:
Ok, I'm reading "The C Programming Language: 2nd Edition"


You do realize that this book is over 15 years old and that the C
standard has been substantially revised since it was written? Try
this instead:

http://www.amazon.com/exec/obidos/ASIN/013089592X
http://www.careferencemanual.com/

DES
--
Dag-Erling Smørgrav - de*@des.no
Nov 14 '05 #4
""Dag-Erling Smørgrav"" <de*@des.no> wrote:
"m_a_t_t" <no******@gmail.com> writes:
Ok, I'm reading "The C Programming Language: 2nd Edition"


You do realize that this book is over 15 years old and that the C
standard has been substantially revised since it was written? Try
this instead:

http://www.amazon.com/exec/obidos/ASIN/013089592X
http://www.careferencemanual.com/


What's your point? Yes, there is a new C standard. Is it your guess that
the OP actually *has* one of these curosities? If he doesn't (which I would
guess at 99% certain) don't you think he would be better off with a book
that matches his compiler? IOW, the one he already has?

That is not meant to imply (nor does it), that the book you are touting is
not a fine book, for it's intended audience.
Nov 14 '05 #5
On Tue, 19 Apr 2005 12:46:58 +0200, de*@des.no (Dag-Erling Smørgrav)
wrote:
"m_a_t_t" <no******@gmail.com> writes:
Ok, I'm reading "The C Programming Language: 2nd Edition"


You do realize that this book is over 15 years old and that the C
standard has been substantially revised since it was written? Try
this instead:

http://www.amazon.com/exec/obidos/ASIN/013089592X
http://www.careferencemanual.com/

DES


No. Harbison & Steele is a fine addition to your references *after*
you learn C, but K&R is much better as a tutorial, and is still
relevant, especially since C99 compilers are very rare.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #6
Groovy hepcat m_a_t_t was jivin' on 18 Apr 2005 14:48:02 -0700 in
comp.lang.c.
I'm very confused(learning C)'s a cool scene! Dig it!
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on
chapter 1.5.1 and here's the program you're sposed to make:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
int c;

c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
General comment about the state of the World (not directed
specifically to Matt): I'm surprised code with things like "main()"
(as opposed to "int main(void)") and the lack of a return statement
appear in K&R2. But anyhow...
}

Ok, now here's what I'm confused about: I read it all and everything
and I'm not sure what it's sposed to do.
The bit that says "copy input to output" should give you some clue.
If that doesn't, then try reading the text above this code (in the
book). It says, "Given getchar and putchar, you can write a surprising
amount of useful code without knowing anything more about input and
output. The simplest example is a program that copies its input to its
output one character at a time." It then gives a pseudocode breakdown
of the algorithm, thus:

read a character
while (character is not end-of-file indicator)
output the character just read
read a character

I don't mean to be mean, but if you can't figure out the purpose of
such an incredibly simple program, even when it is written in plain
English on the same page, then you'll have no hope of being able to
write and debug more sophisticated programs.
Is that what it's supposed to do? And if so then why doesn't it make a
difference if I take out the "!"(not equal to(I think)).


If you mean that you simply removed the "!" character to make this:

while (c = EOF)

then it does make a difference. It makes a big difference, because now
you're assigning the value EOF to c. You now have no way out of the
loop. And you're sending EOF to the output stream. Not good.
If, on the other hand, you mean that you replaced the inequality
operator (!=) with the equality operator (==), then it should print
nothing unless the first getchar() call returned EOF; in which case
writing it may have strange results.
If you mean something else, please clarify.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1124
by: AMT2K5 | last post by:
Hello folks. I am at the chapter in my textbook towards learning about dynamic memory and am practicing with a few examples and lessons from a variety of places. I am confused about one certain...
90
3744
by: Jhon smith | last post by:
Hi all,Just wondering are there any problems with learning c from older books,as I have picked up some from 1988,1994,1997,1998. By using books of this age(Im on a tight budget)am I going to...
12
1857
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
36
2485
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it...
14
2146
by: Rich | last post by:
Hi, (this is a probably a bit OT here, but comp.lang seems rather desolated, so I'm not sure I would get an answer there. And right now I'm in the middle of learning Python anyway so...) ...
6
1371
by: arnuld | last post by:
hai all, 1st of all this post is not about C++, it is about general programming, problems i am facing in learning the concepts & reflects my experience with C and C++ . i know about functions,...
26
9265
by: K.J.Williams | last post by:
Hello, A friend and I want to learn PHP but we have two totally different programming backgrounds. I have experience with procedural programming in C, and he has experience with Visual BASIC....
8
2224
by: Tom | last post by:
Inserting text into a RichTextBox is a snap! >> textBox1.Text += "Enter string 0000\n"; textBox1.Text += "Enter string 0001\n"; textBox1.Text += "Enter string 0002\n"; textBox1.Text += "Enter...
24
1740
by: Bill Cunningham | last post by:
It is very easy to see what I am trying to do with this code: #include <stdio.h> main(){ char name; printf ("Enter -"); fflush (stdout); fgets (name,200,stdin); printf("is? ",name);}
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.