Thread: comma operator clarification

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    comma operator clarification

    Hi,

    i was writing a program and i suddenly declared like this
    Code:
    int i,char c;
    it gave an error and then i declared like this
    Code:
    int i,int j;
    it still gave me an error. Could some one please point out to me the rule in the draft that makes it to give error, so that i can remember or apply it properly to some other cases.

    Thanks and regards,
    satya

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You can't declare more than one type in the same statement. If the variables are of the same type, just separate them by commas. If the variables are of different type, declare them in separate statements, like:
    Code:
    int i;
    char c;
    and
    Code:
    int i, j;
    Devoted my life to programming...

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Satya View Post
    Hi,
    Could some one please point out to me the rule in the draft that makes it to give error, so that i can remember or apply it properly to some other cases.
    satya
    That's not a use of the comma operator. The section of the standard[1] you should be looking at is 6.7.5 (particularly 6.7.5p6)


    [1] ISO/IEC 9899:201x Committee Draft April 12, 2011 N1570

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by GReaper View Post
    You can't declare more than one type in the same statement.
    That's not true. A compound statement (which is what you get when you place a bunch of declarations and statements within curly braces to form a block) can contain multiple declarations (of both types and variables) as well as other statements. Most other statements can't include any declarations.

    In any event.....

    I don't have a copy of the 2011 standard handy, but the relevant section of the 1999 standard is 6.7. As Hodor said, this is not a usage of the comma operator. Declarations are composed of a single declaration specifier (which specifies types, storage class, etc) and a list of one or more declarators separated by commas (where each declarator includes an identifier - the name of something being declared).

    In a declaration
    Code:
        int i, j;
    the "int" is the declaration specifier, and i and j are both the names of things being declared (i.e. the declarators).

    Whereas, the (incorrect) declaration
    Code:
        int i, char c;
    is invalid because "int" and "char" are both declaration specifiers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by grumpy View Post
    That's not true. A compound statement (which is what you get when you place a bunch of declarations and statements within curly braces to form a block) can contain multiple declarations (of both types and variables) as well as other statements. Most other statements can't include any declarations
    Oh, I see! I didn't have compound statements in mind, though.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comma operator
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 10-20-2010, 05:35 AM
  2. k&r page 63: comma operator
    By alecodd in forum C Programming
    Replies: 3
    Last Post: 09-16-2010, 11:10 PM
  3. C comma operator problem
    By greenberet in forum C Programming
    Replies: 10
    Last Post: 07-22-2007, 02:47 AM
  4. comma operator
    By raghuveerd in forum C Programming
    Replies: 5
    Last Post: 07-09-2007, 04:52 AM
  5. Comma Operator
    By doraiashok in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 08:38 AM