comma operator and assignment operator

Discussion in 'C Programming' started by G Patel, Feb 7, 2005.

  1. G Patel

    G Patel Guest

    Hi,

    I've read a book on C, and I understand how comma operators work, but
    my book didn't say that the comma operators between function arguments
    were not really comma operators (even though it seems obvious to me
    that comma operators would serve no purpose between function
    arguments). As per C, are those commas in function argument lists the
    same comma operators?

    Also, are the =s used in initializations the same as any other
    =assignment operator? I know some people keep saying 'initializations
    and assignments are different!' But if they are truly different, then
    the = used with initializations are not really 'assignment' operators.
    So are they the same operator? If yes, then why do people complain that
    initializations and assignments are different?

    To me the following seem the same at execution time:

    int a = 4;

    vs.

    int a;
    a=4;
     
    G Patel, Feb 7, 2005
    #1
    1. Advertising

  2. G Patel

    infobahn Guest

    G Patel wrote:
    >
    > Hi,
    >
    > I've read a book on C, and I understand how comma operators work, but
    > my book didn't say that the comma operators


    separators

    > between function arguments
    > were not really comma operators (even though it seems obvious to me
    > that comma operators would serve no purpose between function
    > arguments). As per C, are those commas in function argument lists the
    > same comma operators?


    No. See K&R2 p63 paragraph 1.

    > Also, are the =s used in initializations the same as any other
    > =assignment operator?


    Same symbol, different purpose.

    > I know some people keep saying 'initializations
    > and assignments are different!' But if they are truly different, then
    > the = used with initializations are not really 'assignment' operators.
    > So are they the same operator? If yes, then why do people complain that
    > initializations and assignments are different?
    >
    > To me the following seem the same at execution time:
    >
    > int a = 4;
    >
    > vs.
    >
    > int a;
    > a=4;


    Try this:

    char a[] = {1, 2, 3};

    vs.

    char a[];
    a = {1, 2, 3};
     
    infobahn, Feb 7, 2005
    #2
    1. Advertising

  3. G Patel

    Neo Guest

    "G Patel" <> wrote in message
    news:...
    > Hi,
    >
    > I've read a book on C, and I understand how comma operators work, but
    > my book didn't say that the comma operators between function arguments
    > were not really comma operators (even though it seems obvious to me
    > that comma operators would serve no purpose between function
    > arguments). As per C, are those commas in function argument lists the
    > same comma operators?
    >
    > Also, are the =s used in initializations the same as any other
    > =assignment operator? I know some people keep saying 'initializations
    > and assignments are different!' But if they are truly different, then
    > the = used with initializations are not really 'assignment' operators.
    > So are they the same operator? If yes, then why do people complain that
    > initializations and assignments are different?
    >
    > To me the following seem the same at execution time:
    >
    > int a = 4;
    >
    > vs.
    >
    > int a;
    > a=4;


    Have a look at this:

    int status = 1;
    int main(void)
    {
    ....
    }

    differs from

    int status;
    int main(void)
    {
    status = 1;
    ....
    }

    in first program status is initialized with value 1, in second program space
    is reserved for the variable status, value is assigned later when main()
    starts executing.

    -Neo
     
    Neo, Feb 7, 2005
    #3
  4. On Sun, 06 Feb 2005 22:44:35 -0800, G Patel wrote:

    > Hi,
    >
    > I've read a book on C, and I understand how comma operators work, but
    > my book didn't say that the comma operators between function arguments
    > were not really comma operators (even though it seems obvious to me
    > that comma operators would serve no purpose between function
    > arguments). As per C, are those commas in function argument lists the
    > same comma operators?


    No, the comma character like some others is used in various different
    syntactic contexts. Just because it is the same character doesn't imply it
    means the same thing. The contexts include

    expr1, expr2 /* comma operator */
    func(arg1, arg2) /* function call */
    int main(int argc, char *argv) /* function declaration or definition */
    int a, b, c /* declarator list */
    enum { A, B, C } /* enumerator list */
    int a[] = { 1, 2, 3 } /* Initialiser list */

    Another example is the ( and ) characters

    (expr) /* grouping parentheses */
    func(args) /* function call */
    (type)expr /* Cast */

    > Also, are the =s used in initializations the same as any other
    > =assignment operator? I know some people keep saying 'initializations
    > and assignments are different!'


    Yes, again it is a different syntactic construct

    > But if they are truly different, then
    > the = used with initializations are not really 'assignment' operators.


    Correct, although there are some similarities.

    > So are they the same operator? If yes, then why do people complain that
    > initializations and assignments are different?


    Consider for example the initialisatiion

    char a[] = "A string";

    There is no direct equivalent assignment e.g.

    a = "A string"; /* Invalid */

    > To me the following seem the same at execution time:
    >
    > int a = 4;
    >
    > vs.
    >
    > int a;
    > a=4;


    Those do have the same effect, but that's not always true, for example

    static int a = 4;

    and

    static int a;
    a=4;

    do not behave the same.

    Lawrence
     
    Lawrence Kirby, Feb 7, 2005
    #4
  5. On Mon, 7 Feb 2005 13:19:10 +0530, "Neo" <>
    wrote:

    >
    >"G Patel" <> wrote in message
    >news:...
    >> Hi,
    >>
    >> I've read a book on C, and I understand how comma operators work, but
    >> my book didn't say that the comma operators between function arguments
    >> were not really comma operators (even though it seems obvious to me
    >> that comma operators would serve no purpose between function
    >> arguments). As per C, are those commas in function argument lists the
    >> same comma operators?
    >>
    >> Also, are the =s used in initializations the same as any other
    >> =assignment operator? I know some people keep saying 'initializations
    >> and assignments are different!' But if they are truly different, then
    >> the = used with initializations are not really 'assignment' operators.
    >> So are they the same operator? If yes, then why do people complain that
    >> initializations and assignments are different?
    >>
    >> To me the following seem the same at execution time:
    >>
    >> int a = 4;
    >>
    >> vs.
    >>
    >> int a;
    >> a=4;

    >
    >Have a look at this:
    >
    >int status = 1;
    >int main(void)
    >{
    > ....
    >}
    >
    >differs from
    >
    >int status;
    >int main(void)
    >{
    > status = 1;
    > ....
    >}
    >
    >in first program status is initialized with value 1, in second program space
    >is reserved for the variable status, value is assigned later when main()
    >starts executing.
    >


    Actually, in the second, status is initialized to 0 and then its value
    is changed to 1 when main executes.


    <<Remove the del for email>>
     
    Barry Schwarz, Feb 8, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Davis
    Replies:
    7
    Views:
    623
    Paul Davis
    Jul 17, 2003
  2. siliconwafer

    operator precedence with && and comma

    siliconwafer, Jul 29, 2005, in forum: C Programming
    Replies:
    21
    Views:
    654
  3. Chris
    Replies:
    34
    Views:
    1,610
  4. ais523
    Replies:
    9
    Views:
    397
    Kaz Kylheku
    Feb 9, 2008
  5. Hicham Mouline
    Replies:
    2
    Views:
    736
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page