473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

comma expression

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?

May 23 '06 #1
7 2598
JAY wrote:
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?


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.
May 23 '06 #2
"JAY" <ja*****@gmail.com> writes:
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.
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?


--
Keith Thompson (The_Other_Keith) ks***@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.
May 23 '06 #3
On Tue, 23 May 2006 08:17:50 UTC, "JAY" <ja*****@gmail.com> wrote:
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=(a,b);
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!
May 24 '06 #4
JAY
I think in c=a,b;b was redecleared:)
Herbert Rosenau wrote:
On Tue, 23 May 2006 08:17:50 UTC, "JAY" <ja*****@gmail.com> wrote:
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=(a,b);


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!


Jun 13 '06 #5
JAY
I think in c=a,b;b was redecleared:)
Herbert Rosenau wrote:
On Tue, 23 May 2006 08:17:50 UTC, "JAY" <ja*****@gmail.com> wrote:
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=(a,b);


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!


Jun 13 '06 #6
JAY
I think in c=a,b;b was redecleared:)
Herbert Rosenau wrote:
On Tue, 23 May 2006 08:17:50 UTC, "JAY" <ja*****@gmail.com> wrote:
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=(a,b);


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!


Jun 13 '06 #7
"JAY" <ja*****@gmail.com> writes:
[...]
On Tue, 23 May 2006 08:17:50 UTC, "JAY" <ja*****@gmail.com> wrote:
> There is an example which confuses me,
> int main()
> {
> int a,b,c,d;
> a=3;
> b=5;
> c=a,b;

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.

--
Keith Thompson (The_Other_Keith) ks***@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.
Jun 13 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: benben | last post by:
I am looking for a good example of overloading operator , (operator comma) Any suggestions? Ben
7
by: Koster | last post by:
Hi folks, As I understand it, amongst other things, the comma operator may be used to cause any number of expressions to be evaluated (who's results are thrown away except the last) where only...
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
2
by: s.subbarayan | last post by:
Dear all, What does this following piece of code do? #define CONVERT_SELF_PTR(type,in,out) \ (((in) != NULL) ? ((out) = (type *)(in), RETURN_OK) : ERROR_PARAM) especially can someone let...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
5
by: Sriram Rajagopalan | last post by:
Hi, Is the extra comma at the end of an enumerator-list valid according to the C standards? With the gcc compiler the following is valid: enum DAYS {MONDAY, TUESDAY, }day1; gcc does not...
1
by: Bosconian | last post by:
I need a regular expression that will validate a double quote comma delimited list where the odd entries are numeric and the even are alphabetical. Each pair must also be on a separate line. For...
4
by: =?Utf-8?B?RXJpY2E=?= | last post by:
I am trying to dynamically create a javascript link. But, I get the following error: BC32017: Comma, ')', or a valid expression continuation expected. Here is the line I try creating the...
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.