473,756 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2626
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_Keit h) 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_Keit h) 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
2294
by: benben | last post by:
I am looking for a good example of overloading operator , (operator comma) Any suggestions? Ben
7
2964
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 one is expected, without the use of braces. So if (condition) i = 0, j = 1; causes both i = 0 and j = 1 expressions to be evaluated if condition evaluates to true. So far just making sure my understanding of the comma
11
2391
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 state\n"), TRUE) : \ (state = newstate, FALSE) This macro is called like this: setState(state, ST_Used);
2
2489
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 me understand why theres a comma in between (out) = (type *)(in) and RETURN_OK in the expression ((out) = (type
21
3041
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 opposed to what is mentioned in precedence table? Also, with comma operator. consider,
5
4529
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 even *warn* about the extra comma after "TUESDAY".
1
1821
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 example: "1","Peter" "2","Paul" "3","Mary" I've used the following expression to validate comma delimited lists, but without the double quotes, numeric/alpha pairing and line return
4
8476
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 link. I'm done staring at it. Can someone else see what may be the matter. Fresh pair of eyes maybe? Thanks: <a href='javascript:PopIt(<%# chr(39)
15
2633
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 sizeof." I think this rule is easy to understand. Because I can find the contexts of applying the rule as follows. (1) int* p = 0; int b1 = sizeof(*p); // OK, b1 = 4, *p would not be evaluated.
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.