Thread: K&R symbolic constants, beginner example

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Just out of curiosity did you check if the book has an errata for that particular section/exercise? Also do you know that there is an "answer" book for all the exercises available.

    What edition of the book are you reading, if not second edition then put in a glass case since it only covers pre-standard C.?

    And no matter the version do you realize that that book is quite ancient (1988, 1978) and really doesn't cover modern C?

    What is your experience level? If you're a beginner then you really should consider finding a more modern book that has good reviews. IMO, that book, because of it's age should really only be used as a "reference" to the language.

    No, he is a human, not a new The Lord.
    Actually he was human, he passed away in 2011.

    Jim

  2. #17
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Kernelpanic View Post
    @Christop - No, No, No, the program from K&R is not correct.

    A program has to terminated with a simple commando. Not with "String-D" or "String-C" or what ever. That's no correct!

    And this original program from the Gurus is without a correct termination. That's a fact!
    It seems like you need to learn some background on Unix philosophy. In Unix, it's extremely common for a program to be written as a "filter" that reads from standard input and writes to standard output (examples are "sort", "cat", "grep", "tr", "sed", "cut", and "awk"). Standard input can come from the terminal, a real file, a pipe, or some other source. The only reliable way for a filter to know when it has reached the end of its input is when it reads "end of file". getchar() returns EOF, fgets() returns NULL, scanf() returns EOF (you need to check whether it's actually EOF or just a conversion error), etc. Many of the programs in K&R reflect this "filter" mentality.

    Furthermore, it's good practice to support any valid input. Who are you to say that "0" is not a valid "word"? Why would you consider, for example, "3" to be a valid "word" but not "0"? The nice thing about EOF is that it's not an actual character that will ever appear inside a file; it can only mean "end of file".

    Try this command in a terminal:

    *nix:
    Code:
    echo hello | ./program
    Windows (because it's backwards):
    Code:
    echo hello | .\program
    A program that looks for a '0' character (like yours) will hang. A program that looks for EOF (as K&R prescribes) will properly terminate as expected.

  3. #18
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    It should be noted that the book apparently neglects to tell people how to signal end-of-file from the keyboard. Although the key-combo is different on different systems, the book doesn't even say "ask your local guru".

  4. #19
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Quote Originally Posted by jimblumberg View Post
    Just out of curiosity did you check if the book has an errata for that particular section/exercise? Also do you know that there is an "answer" book for all the exercises available.

    What edition of the book are you reading, if not second edition then put in a glass case since it only covers pre-standard C.?

    And no matter the version do you realize that that book is quite ancient (1988, 1978) and really doesn't cover modern C?

    What is your experience level? If you're a beginner then you really should consider finding a more modern book that has good reviews. IMO, that book, because of it's age should really only be used as a "reference" to the language.

    Jim
    jimblumberg:

    I started with Absolute beginners guide to C by Greg Perry, also I read (twice, 700 pages) "SAM's teach yourself C programming in one hour a day seventh editon covering C11", by Bradley L. Jones the second time going through and completing all the quiz & exercises taking me less than three months. I have yet to completely finish the last question in the book (SAMS Teach Yourself C), which includes writing a program using the skills l learned in the book. I have started the program, a menu driven program to inventory groceries I have in my freezer & dry storage, writing the input from my FreeBSD terminal to a text file. With the use of the C board Programming forum, tutorialspoint, I feel confident I am capable of writing such a program. I had a goal of going onto K&R ANSI C 2.0 afterward, which I have started


    I have retained much if not all of the information of the syntax to the beginner books, I paged through "Let Us C" and understood the inforamtion and paging through the SMS book listed above I remember much of the information if not all of it,


    I had a goal of going onto K&R ANSI C 2.0 afterward, which I have started. I also have the Answer's Book, I stopped at page 24 with this question: "Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging." The solution in the Answers Book is too challenging and I would need significant practice to be able to work through all the exercises


    Basically, I can keep K&R for a further date, work on the "grocery inventory" program on my Unix box, using tutoriaspoint, the C programming forum, and practice practice practice. Thanks!


    By the way , it's taken me since Feb 2018 (11 months as of Jan 2019) to get to as far as I am.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It should be noted that the book apparently neglects to tell people how to signal end-of-file from the keyboard.
    So? The book assumes you know something about your computer operating system. At the time that book was written programmers were expected to understand how the operating system and computers in general operated.

    the book doesn't even say "ask your local guru".
    That's because if you were reading the book you were the local guru.

    I started with Absolute beginners guide to C by Greg Perry, also I read (twice, 700 pages)
    I hope by read you mean you read a chapter and completed all exercises and typed in most of the sample code and compiled the programs. Just reading a programming book is not how one learns to program. You must write programs to learn.

    With the use of the C board Programming forum, tutorialspoint, I feel confident I am capable of writing such a program.
    Then do it! As I said you learn programming by programming. And when you start your program you need to do most if not all the work yourself. It is alright to refer to documentation but design and write the program yourself don't try to take a bunch of snippets from somewhere else to accomplish the goals. Again you learn by doing.

    I paged through "Let Us C"
    Well just "paging" through a book doesn't really accomplish much. Again you need to program in order to learn to program.

    My problem with the K&R book is it's age. Modern C (ISO C11) has changed quite a bit since ANSI C. There are many constructs that are no longer allowed in modern C, default argument types for example, so I really recommend saving that book until you understand more modern C. And then view it as a "history book", not a "how to program" book.

  6. #21
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Quote Originally Posted by jimblumberg View Post
    So? The book assumes you know something about your computer operating system. At the time that book was written programmers were expected to understand how the operating system and computers in general operated.
    WTF?!
    I was just pointing out a fact.
    You have a problem, buddy!

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I was just pointing out a fact.
    No, you were just whining.

  8. #23
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by jimblumberg View Post
    No, you were just whining.
    You do exactly what I can not stand, you turn a job into an ideology.

    Programming is also nothing more than building a scaffolding clean without it collapsing, or a mechanician who repairing a car, a motor, and so on . . .

    Programming is not a religion and not a church it is only a job, a job, not more not fewer. But it is absolutly not a religious affiliation. This article from 2004 by @Telepolis fits with that, I think so. In German only.

    Luftbuchungen der freien Softwareszene | Telepolis

  9. #24
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by christop View Post
    It seems like you need to learn some background on Unix philosophy. . . .
    I know the Linux(Unix)-philosophy since 1996; a little bit. Windows was always my first system but Linux, SuSE-Linux, was a hobby from 1996 to 2010/2011. And more, I was a sponsor of Linux - I have buy all boxes from SuSE 4.3 to SuSE 9.3. Yes!

    That's my first screenshot from Linux - I think yes:

    K&R symbolic constants, beginner example-tocrackornot-jpg

  10. #25
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    OK, so you use Linux on a desktop. That doesn't necessarily mean you understand the Unix philosophies of "everything is a file" and of writing programs as filters.

    My main point was that a program that terminates on EOF (which is a perfectly acceptable termination condition) will work and terminate properly with any type of file, whether it's a terminal, a regular file, a character or block device file, a pipe, a socket, etc, etc, while your program will hang if it doesn't contain a '0' character. Using a specific but arbitrary character to terminate a program looks like the work of a first-year programming student.

    tl;dr: Your program doesn't have a proper termination condition.

    PS. Apparently German keyboards have a "Strg" key, which is short for "Steuerung", which means "Control" in English. That is abbreviated as "Ctrl" on US keyboards. Google Translate even translates "Strg" to "Ctrl". On a German keyboard, EOF is typed with "Strg-D" in Unix/Linux or with "Strg-Z" in Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Symbolic Constants? O_o
    By SCRIPT_KITTEH in forum C Programming
    Replies: 6
    Last Post: 07-19-2013, 11:10 PM
  2. Help with symbolic constants
    By rpmischris in forum C Programming
    Replies: 20
    Last Post: 09-17-2012, 08:15 PM
  3. is there a weird rule about symbolic constants?
    By i_can_do_this in forum C Programming
    Replies: 5
    Last Post: 07-10-2006, 07:14 AM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 08:29 PM
  5. Symbolic Constants??
    By ACAC in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2001, 06:09 PM

Tags for this Thread