tags:

views:

104

answers:

2

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&R and 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...

Terminal

A: 

Looks like you are doing this in Xcode. Not sure how Xcode handles stdin/out and what project template you are using. You should have all this in a simple .c file and just gcc it yourself without Xcode and see what happens then.

Edit: in Unix console, you press Ctrl-D to end stdin input to a command line program. Wikipedia has more.

Jaanus
Hi, thanks for your answer. I'm using the template *command line tool*, and have used this in the past fine with input from the command line.
alex
+2  A: 

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 and sleep(2); right before return 0;, and I can see the correct output.

Here is the complete program:

#include <stdio.h>
#include <unistd.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);
    sleep(2);

    return 0;
}
Alok
Didn't seem to make a difference on mine. Also, apologies if this is a stupid question, but after the `exit` on the CL, I can type whatever I want into Terminal with new lines. Is this standard? Thanks
alex
@alex: it's not a stupid question. The program is waiting to read stuff on standard input, and once it is done reading, it prints out the numbers of lines. So, you enter a bunch of lines, and then type `ctrl-D` to send `EOF` to it. Then, the program reports the number of lines on standard output, and exits. Xcode is running two programs in the terminal: your program, followed by `exit`, which exits the terminal (the shell actually). So as soon as your program prints the output, it's done, and `exit` exits the terminal. I think you're confused about how things are working. ...
Alok
@alex: I suggest you compile my program in Xcode, run it, and then type a bunch of lines in the open terminal, followed by `ctrl-D`. You will then see the output, and then the terminal will wait for about 2 seconds and then go away. Or, you can open a terminal manually, go to the directory containing the program, run the program, and then do what I suggested above.
Alok