Thread: back to basics kernigan and ritchie book

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    back to basics kernigan and ritchie book

    hi all, I have decided to go back to basics because i seem to be missing some fundamental steps somewhere. I am working my way though the k&r book and have come across a sample code that doesn't work for me (it should work because the exercise is to modify it to find the value of EOF

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int c;
    
        c=getchar();
    
        while (c != EOF)
        {
            putchar(c);
            c=getchar();
        }
    
        return 0;
    }
    when i run this i get stuck in an endless loop ie EOF is never found

    many thanks
    Coop

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,714
    EOF is signalled by either ctrl-d (Unix/Linux) or ctrl-z (Windows)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Quote Originally Posted by Salem View Post
    EOF is signalled by either ctrl-d (Unix/Linux) or ctrl-z (Windows)
    is this limited to the console or is EOF always ctrl-d
    ie if i built and distributed the program would whoever running it have to know this to break the loop

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,608
    if i built and distributed the program would whoever running it have to know this to break the loop
    Probably not, they probably wouldn't even know that they were supposed to "break out of the loop", after all you didn't know how to break out of the loop.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    784
    If you redirected this program to read from a file, it would stop when it reaches the end of the file. Otherwise, from a terminal, the only way to signal EOF is with Ctrl-D or Ctrl-Z, as Salem mentioned (closing the terminal window should work too).

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    Ok another issue is how does getchar and putchar work. I have to press enter to get it to read in the string then it puts it back out is this correct

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,608
    Well getchar() is retrieving a single character (as an int) from the console (stdin), putchar() puts a single character to the console (stdout). Note there are no strings involved with either of these functions. Did you try finding and reading some documentation for these standard functions.

    By the way be careful trying to use getchar() with a char, it is possible that EOF can not be held in a char.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    The normal thing to do is to prompt the user with something like "... or 'q' to quit"


    Also remember that K&R C is not ANSI C - There are differences
    ANSI-C Vs K&R-C - C Tutorials - Sanfoundry

    And (personally) as a rule I avoid any compiler that doesn't support ANSI C (or ISO).


    If you are also going to be using ANSI C, I suggest that you learn only that.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    im not explaining myself i think.

    i have the following code
    Code:
    int c;
    
    while ((c = getchar()) != EOF)
    {
          putchar(c);
    }
    return 0;
    when run it opens a console (xterm) that i can type into but i don't get an output until i hit the enter key. ie rather than getting hheelllloo i get
    hello
    hello

    so assessing a single character on a string that is 1 character long boggles the mind (yes i know a string is more than one character)

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    Quote Originally Posted by cooper1200 View Post
    when run it opens a console (xterm) that i can type into but i don't get an output until i hit the enter key
    That is how getchar works

    If you'd like to explore ways of getting input without hitting enter...
    How can I get input without having the user hit [Enter]? - Cprogramming.com

    Be warned that you will lose portability though
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    the exersise is asking me to replace tabs with \t backspaces with \b and \ with \\ using getchar and putchar while i can detect tabs and and \ after the enter key has been presses how can i detect backspaces as the "string"has already been modified

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    The getchar function waits for a \n to be detected, after that it will read to the end of the stream one character at a time.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    As long as you're starting over, why not pick up a copy of the K. N. King book "C Programming: A Modern Approach?" The K&R book is venerable, but it can be pretty terse at times. I don't know of anything better than the K. N. King book.

    As for your issues, you're running into problems with both buffered input and closing stdin. Because input is buffered on a platform like Linux with xterm, when you call a function like getchar() or scanf(), the program will wait not only until the user hits a key, but also until they hit enter. That data is not even sent to the program until the user hits enter. So your program not continuing until you hit enter is normal. There are ways to do "raw" input, but it gets real hairy so it's best you just put that thought aside for now.

    Yes, this program will loop until you hit Ctrl-D. Ctrl-D tells the terminal that you're finished giving input to this program. The stdin input stream that things like getchar() and scanf() read from will be closed, and any input remaining in the buffer will be processed as if you hit enter (but without inserting a \n into the stream). When they get to the end of the stream, getchar() will return EOF and this is where the loop in your program will end.

  14. #14
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    last thing on this question i promise then im throwing this bloody book on the bonfire
    i am being asked to write a ptogram that detects tabs, backspaces and back slashes and replace them with \t \b \\ respectivly. this is what i have so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int c;
    
        while ((c = getchar()) != EOF)
        {
            if (c == '\t')  // find a tab (\t) and change it to "\\t"
            {
                printf("\\t");
            }
            else if (c == '\b')
            {
                printf("\\b");
            }
            else if (c == '\\')
            {
                printf("\\\\");
            }
            else
            {
                putchar(c);
            }
        }
        return 0;
    }
    this works concerning the tab and the backslash however the backspace is never executed. if i manually type in \b it reads it in separately ie i get \\b as an output

    many thanks
    Coop

  15. #15
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    You're having terminal problems again. If you type a backspace, the terminal driver will remove the last buffered character. In most terminals to type a literal backspace and send it to the program along with the other text, you can try Ctrl-V backspace (the key, don't type backspace). This is if you're on Linux or similar, though. If you're on Windows, I have no idea.

    Your program looks fine, though. I wouldn't worry about this, it's a bit of a strange thing to encounter a backspace character in your input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem from book The C Programming Language by Ritchie
    By Thomas Elder in forum C Programming
    Replies: 5
    Last Post: 12-21-2016, 01:54 PM
  2. Kerninghan and Ritchie book, comments or reviews?
    By Terrance in forum Programming Book and Product Reviews
    Replies: 3
    Last Post: 11-08-2016, 02:56 AM
  3. Kernigan and Ritchie Exercise 5-11
    By coder222 in forum C Programming
    Replies: 2
    Last Post: 11-26-2015, 02:40 PM
  4. Kernigan and Ritchie Book Question!
    By Matus in forum C Programming
    Replies: 10
    Last Post: 11-06-2008, 11:58 AM
  5. Newbie question: Kernighan-Ritchie C book- problem
    By plutonas in forum C Programming
    Replies: 2
    Last Post: 06-29-2005, 12:50 PM

Tags for this Thread