473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I'm very confused(learni ng 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 1821


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(learni ng 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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #7

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

Similar topics

1
1140
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 function within my practice question, it reads "int grow(int s = 10) - a private function that allocates memory to savings. If savings is NULL allocate enough memory to hold s Accounts. If savings is not NULL increase by s the size of the array...
90
3887
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 missout on anything in the langauge or has C remaind similar. I intend to use Dev-C++ on the windows platform. If any one feels theres anything I should be aware of,please help me out,I feel a bit lost with all thats out there regarding this language....
12
1890
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 the build fails with what the book tells me to do. Specifically, I am doing this: public authors1 GetAuthors() { authors1 authors = new Authors1();
36
2547
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 seems that it discusses the std and the other part of the language with your own abstractions. Is that better to read a book first on the basic concepts of C++ language (but not the C part) that gives the basics as if the reader is a beginner...
14
2172
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...) Anyway, my question is: what experience you people have with working with different languages at the same time? Actually I did myself many years ago, on my Commodore machines, where
6
1392
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, variables, compilers, interpreters etc etc but i have never done any real-life coding. i am trying to learn C++ as most of the jobs in my area are for the graduates/PGs carrying these skills: 1.) C++
26
9321
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. Well we wanted to know, what type of learning curve ( of difficulty ) we would have trying to learn PHP? Also, What will be the most significant changes for us to adapt to? I wanted to know if PHP is like
8
2251
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 string 0003\n"; How do I then go back and programmatically remove let's say line #2? As amazingly simple as this sounds ... I am equally dumbfounded by not
24
1783
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
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 we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.