![]() |
comma operator and assignment operator
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; |
Re: comma operator and assignment operator
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}; |
Re: comma operator and assignment operator
"G Patel" <gaya.patel@gmail.com> wrote in message news:1107758675.040544.187430@l41g2000cwc.googlegr oups.com... > 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 |
Re: comma operator and assignment operator
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 |
Re: comma operator and assignment operator
On Mon, 7 Feb 2005 13:19:10 +0530, "Neo" <timeless_illusion@yahoo.com>
wrote: > >"G Patel" <gaya.patel@gmail.com> wrote in message >news:1107758675.040544.187430@l41g2000cwc.googleg roups.com... >> 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>> |
| All times are GMT. The time now is 01:00 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.