Thread: k&r page 63: comma operator

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    19

    k&r page 63: comma operator

    can somebody explain to me the meaning of the following paragraph?

    "Comma operators should be used sparingly. The most suitable uses are for constructs strongly related to each other, as in the for loop in reverse, and in macros where a multistep computation has to be single expression." (K&R page 63).

    Where and why is the comma operator necessary in macro's definition. Is there any difference if the macro expands to a comma separated pair of expression, or a semi-colon separated pair of expression? I don't so...
    Then, when does a "multistep computation in a macro HAVE to be a single expression"?

    thanks
    alex

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by alecodd View Post
    Then, when does a "multistep computation in a macro HAVE to be a single expression"?
    What if you wanted to use the macro as the conditional in a while loop? Then it needs to be a single expression.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    Quote Originally Posted by bithub View Post
    What if you wanted to use the macro as the conditional in a while loop? Then it needs to be a single expression.
    why not use braces and then it just become a block by itself..or is it just another alternative to use without introducing a new scope by a nested block?

    thnaks

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by alecodd View Post
    why not use braces and then it just become a block by itself..or is it just another alternative to use without introducing a new scope by a nested block?

    thnaks
    A block is not an expression. You can't put a block inside a conditional.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some Interview questions, please help
    By higaea in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2010, 06:35 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. K&R problem page
    By Blur in forum C Programming
    Replies: 4
    Last Post: 08-31-2005, 09:38 AM
  4. virtual memory
    By sweets in forum C Programming
    Replies: 6
    Last Post: 11-06-2004, 06:55 AM
  5. C Graphics - Page Flipping
    By z0diac in forum C Programming
    Replies: 1
    Last Post: 10-29-2002, 01:21 AM