Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comma operator and assignment operator

Reply
Thread Tools

comma operator and assignment operator

 
 
G Patel
Guest
Posts: n/a
 
      02-07-2005
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;

 
Reply With Quote
 
 
 
 
infobahn
Guest
Posts: n/a
 
      02-07-2005
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};
 
Reply With Quote
 
 
 
 
Neo
Guest
Posts: n/a
 
      02-07-2005

"G Patel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      02-07-2005
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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-08-2005
On Mon, 7 Feb 2005 13:19:10 +0530, "Neo" <(E-Mail Removed)>
wrote:

>
>"G Patel" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed) 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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
The comma operator, and assigning twice between sequence points ais523 C Programming 9 02-09-2008 07:18 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
operator precedence with && and comma siliconwafer C Programming 21 08-10-2005 12:27 AM



Advertisments