Hello, ladies, gentlemen.
First I suppose I should introduce myself.
I've been poking at C since a long time ago, somewhere around 1990. (Don't misinterpret that. "Poking at C", in this statement, means that I jumped on it, studied it for anything from a day to a weekend to a finished "Hello, World" program, then forgot about it while years of life roared by. The statement is not meant to imply that I have years and years and years of programming, rather, it simply means that lots and lots of time went by while my programming skills stood still.) I'm not a professional, I'm not a student, and I don't really know that much about it, other than this: it fascinates me.
My knowledge of C and programming comes from these disjointed forays, books, the 'net, and the occasional burst of help from friends who are REAL programmers.
The difference between us is profound. Given a project, they sit down and methodically start laying out a process to complete the solution, while I sit down and start eliminating ways that don't work. Their way is faster.
I program on a Mac, currently with OS X, (10.6.2, Snow Leopard). I use Xcode as my environment, and I compile C with Terminal and everything else with Xcode.
When I started, the Big Thing was CodeWarrior, and I never learned to hammer out code on a Unix platform. In fact, because the Mac environment was so different then, I never learned to do a lot of basic stuff.
Of course, back in the day, I bought a copy of K&R's The C Programming Language, and I've benefitted from it immensely.
I've always looked at the exercises, and promised myself that someday, I would come back and see to them.
That day has arrived.
I just (two nights ago, in fact) figured out how to use Xcode to write C files, and how to use the cc command in Terminal to compile them. After a big search, I figured out that "./a.out" runs the output, and I became able, for the first time, to write, compile and run programs that are suitable (in my unlearned opinion) for the K&R exercises.
I know that programming has advanced, and I know that Computer Years are even more extreme than Dog Years; I know that Objective C is what Xcode is all about now, and I remember pulling my hair out while I tried to write code to get a window to scroll, a task that Interface Builder handles auto-magically now.
Still, I've always felt that if I understood ANSI C better, my understanding of programming in general would be better, so I've decided to work my way through the K&R book, and hunt down the answers to every stupid little exercise.
So.
I managed to make it through the first five exercises, and I have code that should solve 1-6, and I solved 1-7.
However. (You knew that was coming, didn't you?)
The exercises that center on getchar and putchar puzzle me: I don't see where they are getting the text stream, and I believe that it is coming from the keyboard.
That creates a couple problems, first is that I don't know what to type to generate an EOF character, so my programs (and one of their programs) are endless loops, and you have to close the Terminal window to get them to quit.
This code is also a problem:
// Test.c
#include <stdio.h>
//Verify that the expression "getchar() != EOF" is 0 or 1.
//Exercise 1-6 on page 17.
main() {
int c;
c = getchar();
while (c != EOF) {
while (c != 1) {
while (c != 0) {// You only get here if c is not 1.
printf("EOF is not 1 or 0."); //c is not 1 or zero.
} //end while test for zero
} // end while test for 1
printf("\nThe value passed, because EOF is ");
printf("%d\n", c);
}
}
When I run it, it doesn't appear to do anything, until I type something. Then it enters an endless loop, and doesn't exit unless I close the Terminal window.
I don't see how I'm creating a loop.
I also don't see (in the K&R book) how to obtain a text stream other than from the keyboard.
Any help will be welcome.
This code is a different type of problem:
// count.c
#include <stdio.h>
// count characters in input, page 18 of C book, version 1
main() {
long nc;
nc = 0;
while (getchar()!=EOF) {
++nc;
printf("%ld\n", nc);
} // end of while
} // end of main
This code is straight out of the K&R book. What it does is nothing, until you type something. Then it generates a random number of numbers. It also doesn't quit unless you quit the Terminal window.
I appreciate your looking. Thank you.