comma expression

J

JAY

There is an example which confuses me,
int main()
{
int a,b,c,d;
a=3;
b=5;
c=a,b;
d=(a,b);
printf("c=%d",c);
printf("d=%d",d);
return 0;
}
I think the reuslt should be 5,5 but it is 3,5
how the comma expression works?
 
K

Keith Thompson

JAY said:
There is an example which confuses me,
int main()
{
int a,b,c,d;
a=3;
b=5;
c=a,b;

This is equivalent to "(c=a),b;". "=" is also an operator, and it
binds more tightly than the comma operator.
 
H

Herbert Rosenau

There is an example which confuses me,
int main()
{
int a,b,c,d;
a=3;
b=5;
c=a,b;

The compiler reads this as (c=a),b; Means c gets set with the value of
a, The expression of b gets lost.

d gets the with the result of the expression a,b. That means a gets
calculated and forgotten, then b gets calculated and delivers its
result to the = operator.
printf("c=%d",c);

No comma operator at all. There is only a parameter separator.
printf("d=%d",d);

No comma operator at all. There is only a parameter separator.
return 0;
}
I think the reuslt should be 5,5 but it is 3,5
how the comma expression works?
You have to learn how a C compiler reads the code. The standard
declares that clearly.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
J

JAY

I think in c=a,b;b was redecleared:)
Herbert said:
The compiler reads this as (c=a),b; Means c gets set with the value of
a, The expression of b gets lost.


d gets the with the result of the expression a,b. That means a gets
calculated and forgotten, then b gets calculated and delivers its
result to the = operator.


No comma operator at all. There is only a parameter separator.


No comma operator at all. There is only a parameter separator.

You have to learn how a C compiler reads the code. The standard
declares that clearly.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
J

JAY

I think in c=a,b;b was redecleared:)
Herbert said:
The compiler reads this as (c=a),b; Means c gets set with the value of
a, The expression of b gets lost.


d gets the with the result of the expression a,b. That means a gets
calculated and forgotten, then b gets calculated and delivers its
result to the = operator.


No comma operator at all. There is only a parameter separator.


No comma operator at all. There is only a parameter separator.

You have to learn how a C compiler reads the code. The standard
declares that clearly.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
J

JAY

I think in c=a,b;b was redecleared:)
Herbert said:
The compiler reads this as (c=a),b; Means c gets set with the value of
a, The expression of b gets lost.


d gets the with the result of the expression a,b. That means a gets
calculated and forgotten, then b gets calculated and delivers its
result to the = operator.


No comma operator at all. There is only a parameter separator.


No comma operator at all. There is only a parameter separator.

You have to learn how a C compiler reads the code. The standard
declares that clearly.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
K

Keith Thompson

JAY said:
I think in c=a,b;b was redecleared:)

Please don't top-post. I've corrected it here.
See <http://www.caliburn.nl/topposting.html>.

You posted the same thing 3 times. Please be careful about that.

No, b is not redeclared; what makes you think it is? The line
c=a,b;
is a statement, not a declaration. It's equivalent to
(c=a),b;
The assignment "c=a" assigns the value of a to c, and yields the value
assigned. The comma operator evalutes its left operand (the
assignment), discards its result, then evaluates its right operand (b)
and yields the result of that evaluation. The full expression "c=a,b"
is followed by a semicolon, making it an expression statement; the
expression is evaluated and its result is discarded. The net result
is that the value of a is assigned to c; b is accessed and ignored.
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top