I’ve just typed in an example from K&R…
#include <stdio.h>
int main (int argc, const char * argv[]) {
int c, nl;
nl = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') {
++nl;
}
}
printf("new lines=> %d\n", nl);
return 0;
}
However, when I build & run, I get…
minimac:~ alex$ /Users/alex/Documents/K\&R/build/Debug/K\&R ; exit;
As you may be able to gather, running off the Mac OS X Terminal, if that means anything.
Why doesn’t this ever prompt for input?
Update
Here is how I started my project, on Mac OS X Snow Leopard
- Ran Xcode
- Started new project “command line tool” and called it
K&R - Typed in the code into
main.c - Hit the big button above “build & run”
- Double clicked
K&Rand Terminal was launched with the output above
I might also state that I have been using interpreted languages pretty much my entire life, so I am new to this compiling process.
When I use gcc to compile from the Terminal, I can run the program with ./a.out. However, once I type, I don’t know how to tell the program I’m done, now please tell me how many lines have I typed in.
Here is a screenshot of my Terminal…

(source: alexanderdickson.com)
OK, I just did what you said in your steps, and I am able to type into the terminal. However, before I get to see the output from the program, the terminal is closed (because of the
exit;). I added a#include <unistd.h>at the beginning andsleep(2);right beforereturn 0;, and I can see the correct output.Here is the complete program: