comma operator and assignment operator

G

G Patel

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;
 
I

infobahn

G said:
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};
 
N

Neo

G Patel said:
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
 
L

Lawrence Kirby

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
 
B

Barry Schwarz

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>>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top