new to C


 
Thread Tools Search this Thread
Top Forums Programming new to C
# 1  
Old 12-06-2002
new to C

although i'm new to programming in C i'm not new to programming. Normally i program in Pl/1 or cobol on a mainframe.
I wanted to learn C so i bought myself the book the C programming language. I know it's a simple program but i want to learn and understand the language so that's why
i want to know why it isn't working as i expected.
one of the examples in the book is the one below
Code:
#include <stdio.h>
main()
{
    int c;
    c = getchar();
     while (c != EOF) {
          putchar(c);
          c =getchar();
     }
}

first when i type -1 (i thought is EOF) the program doesn't stop.(At least used printf to show what EOF has as a value).
Second i created the one below. I guessed after two loops the program stops but it doesn't could someone explain to me why ??? (i use gcc on linux 8.0 to compile )
Code:
#include <stdio.h>

main()
{
        int c, i ;
        c = 0;
        i = 0;
        while ( c != EOF) {
                c = getchar();
                putchar(c);
                i++;
                printf("i has value %d", i);
                if (i == 2){
                        c = EOF;
                        printf("\nend of file #%d#\n", c);
                }
        }
}

(I added code tags so we can see the indentation... Perderabo)

Last edited by Perderabo; 12-06-2002 at 06:07 PM..
# 2  
Old 12-06-2002
EOF is typically Ctrl-D. To find out what yours is mapped to, use the command "stty -a", and look for "stty = something". In my case, it shows "^D".

If it isn't set to ^D, you can actually type it two ways in Linux (not sure about others - also, by "linux 8.0", I assume you mean Redhat 8.0):
stty eof "^D" ( That's "caret" then "D")
or
stty eof ^D (That's "Ctrl-V" then "Ctrl-D")

Hope this helps, at least for your first question.
# 3  
Old 12-06-2002
thnx that's indeed the first part and it must be suse 8.0 Smilie
Now if someone could explain the second part why my own version isn't looping twice but just once i'm very gratefull.
# 4  
Old 12-06-2002
First, to clarify EOF, cntl-D is the convention to tell the terminal driver that there is an end of file. But if you are reading from a disk file or something, EOF occurs when you run out of data. A cntl-D in a data file is just another character. And in either case, a -1 is what getchar will return to indicate eof.

Second, your program loops twice for me. It may not look like it, because you need a \n in your first printf, just like you have in your second printf.
# 5  
Old 12-07-2002
sorry my question was wrong it loops twice but it only asks once for input.
that's the part i do not understand.
An other thing is i get an -1 as EOF but only control-D is recognized as EOF.
# 6  
Old 12-07-2002
You have a getchar in your loop. Each iteration on the loop will get a single character. If you literally type "-1 <return>", you will get a "-" on the iteration, a "1" on the second iteration, and a newline on the third iteration. On the fourth iteration, it must get another line from the keyboard.

I explained the -1 verses cntl D as best I could in my first post.
# 7  
Old 12-20-2002
CPU & Memory

Better u follow the book named "LET US C" 4th edition , by Y.P.Kanithker(India).
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question