K&R book question

This is a discussion on K&R book question within the C Programming forums, part of the General Programming Boards category; I'm reading this magnificent book and I came across a paragraph that I do not understand. It's the last paragraph ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    K&R book question

    I'm reading this magnificent book and I came across a paragraph that I do not understand. It's the last paragraph on page 39.

    "Enumerations provide a convenient way to associate constant values with names, an alternative to #define with the advantage that the values can be generated for you. Although variables of enum types may be declared, compilers need not check that what you store in such a variable is a valid value for the enumeration. Nevertheless, enumeration variables offer the chance of checking and so are often better than #defines. In addition, a debugger may be able to print values of enumeration variables in their symbolic form. "

    Can someone rephrase this for me? Thanks a lot.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Code:
    // good
    enum { ORANGE, APPLE, PEAR };
    typedef enum { RED, AMBER, GREEN } light;
    light lamp = RED;
    
    // bad
    #define RED 0
    #define AMBER 1
    #define GREEN 2
    int lamp = RED;
    > an alternative to #define with the advantage that the values can be generated for you.
    You don't have to invent all the numbers, imagine having to number several dozen of them.
    Then imagine say having to renumber them when you want to add something else.

    > Nevertheless, enumeration variables offer the chance of checking and so are often better than #defines
    In the former case, then compiler might be able to tell you about dumb things like
    lamp = 42;
    lamp = ORANGE;
    A #define cannot do this.

    > In addition, a debugger may be able to print values of enumeration variables in their symbolic form. "
    Given
    int lamp = RED;
    You'd always see 0 when printed inside the debugger - then you'd have to go look it up yourself
    light lamp = RED;
    Good debuggers will print RED when you examine the variable.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggest a book
    By Roelof92 in forum Tech Board
    Replies: 3
    Last Post: 02-05-2009, 08:43 PM
  2. Question about a C book
    By Poincare in forum C Programming
    Replies: 7
    Last Post: 02-01-2009, 12:50 PM
  3. ascii backspace, K&R book exercises
    By jjohhn in forum C Programming
    Replies: 10
    Last Post: 12-29-2005, 07:33 AM
  4. basic recursion function question. typo in book?
    By navyzack in forum C Programming
    Replies: 6
    Last Post: 04-18-2005, 11:07 AM
  5. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21