comma expression | | |
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? | | | | re: comma expression
JAY wrote:[color=blue]
> 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?[/color]
The comma operator has the lowest precedence of all operators,
so that you can write "c = a, d = b". http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
--
post tenebras lux. post fenestras tux. | | | | re: comma expression
"JAY" <jayhaan@gmail.com> writes:[color=blue]
> There is an example which confuses me,
> int main()
> {
> int a,b,c,d;
> a=3;
> b=5;
> c=a,b;[/color]
This is equivalent to "(c=a),b;". "=" is also an operator, and it
binds more tightly than the comma operator.
[color=blue]
> 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?[/color]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: comma expression
On Tue, 23 May 2006 08:17:50 UTC, "JAY" <jayhaan@gmail.com> wrote:
[color=blue]
> There is an example which confuses me,
> int main()
> {
> int a,b,c,d;
> a=3;
> b=5;
> c=a,b;[/color]
The compiler reads this as (c=a),b; Means c gets set with the value of
a, The expression of b gets lost.
[color=blue]
> d=(a,b);[/color]
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.
[color=blue]
> printf("c=%d",c);[/color]
No comma operator at all. There is only a parameter separator.
[color=blue]
> printf("d=%d",d);[/color]
No comma operator at all. There is only a parameter separator.
[color=blue]
> return 0;
> }
> I think the reuslt should be 5,5 but it is 3,5
> how the comma expression works?
>[/color]
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! | | | | re: comma expression
I think in c=a,b;b was redecleared:)
Herbert Rosenau wrote:[color=blue]
> On Tue, 23 May 2006 08:17:50 UTC, "JAY" <jayhaan@gmail.com> wrote:
>[color=green]
> > There is an example which confuses me,
> > int main()
> > {
> > int a,b,c,d;
> > a=3;
> > b=5;
> > c=a,b;[/color]
>
> The compiler reads this as (c=a),b; Means c gets set with the value of
> a, The expression of b gets lost.
>[color=green]
> > d=(a,b);[/color]
>
> 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.
>[color=green]
> > printf("c=%d",c);[/color]
>
> No comma operator at all. There is only a parameter separator.
>[color=green]
> > printf("d=%d",d);[/color]
>
> No comma operator at all. There is only a parameter separator.
>[color=green]
> > return 0;
> > }
> > I think the reuslt should be 5,5 but it is 3,5
> > how the comma expression works?
> >[/color]
> 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![/color] | | | | re: comma expression
I think in c=a,b;b was redecleared:)
Herbert Rosenau wrote:[color=blue]
> On Tue, 23 May 2006 08:17:50 UTC, "JAY" <jayhaan@gmail.com> wrote:
>[color=green]
> > There is an example which confuses me,
> > int main()
> > {
> > int a,b,c,d;
> > a=3;
> > b=5;
> > c=a,b;[/color]
>
> The compiler reads this as (c=a),b; Means c gets set with the value of
> a, The expression of b gets lost.
>[color=green]
> > d=(a,b);[/color]
>
> 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.
>[color=green]
> > printf("c=%d",c);[/color]
>
> No comma operator at all. There is only a parameter separator.
>[color=green]
> > printf("d=%d",d);[/color]
>
> No comma operator at all. There is only a parameter separator.
>[color=green]
> > return 0;
> > }
> > I think the reuslt should be 5,5 but it is 3,5
> > how the comma expression works?
> >[/color]
> 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![/color] | | | | re: comma expression
I think in c=a,b;b was redecleared:)
Herbert Rosenau wrote:[color=blue]
> On Tue, 23 May 2006 08:17:50 UTC, "JAY" <jayhaan@gmail.com> wrote:
>[color=green]
> > There is an example which confuses me,
> > int main()
> > {
> > int a,b,c,d;
> > a=3;
> > b=5;
> > c=a,b;[/color]
>
> The compiler reads this as (c=a),b; Means c gets set with the value of
> a, The expression of b gets lost.
>[color=green]
> > d=(a,b);[/color]
>
> 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.
>[color=green]
> > printf("c=%d",c);[/color]
>
> No comma operator at all. There is only a parameter separator.
>[color=green]
> > printf("d=%d",d);[/color]
>
> No comma operator at all. There is only a parameter separator.
>[color=green]
> > return 0;
> > }
> > I think the reuslt should be 5,5 but it is 3,5
> > how the comma expression works?
> >[/color]
> 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![/color] | | | | re: comma expression
"JAY" <jayhaan@gmail.com> writes:
[...][color=blue][color=green]
>> On Tue, 23 May 2006 08:17:50 UTC, "JAY" <jayhaan@gmail.com> wrote:
>>[color=darkred]
>> > There is an example which confuses me,
>> > int main()
>> > {
>> > int a,b,c,d;
>> > a=3;
>> > b=5;
>> > c=a,b;[/color]
>>[/color]
> I think in c=a,b;b was redecleared:)[/color]
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.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|