x=(r,s,t);

K

karthikbalaguru

Hi,

I get a strange output with the below code snippet.

#include<stdio.h>
int main(void)
{
int r=2,s=3,x=0,t=7;
x=(r,s,t);
printf("x values is %d \n",x);
return 0;
}

I get the output as 7.
It looks to be dependent on 't'.
If i change the value of t as 5, the output is 5.

How does it give 7 as ouput ? Any reasons for
the dependency on 't' variable ?
Does C support this kind of assignment of x=(r,s,t);
for any specific reasons ?

Thx in advans,
Karthik Balaguru . T
 
W

Willem

karthikbalaguru wrote:
) I get a strange output with the below code snippet.

What did you expect the output to be ?

) #include<stdio.h>
) int main(void)
) {
) int r=2,s=3,x=0,t=7;
) x=(r,s,t);
) printf("x values is %d \n",x);
) return 0;
) }
)
) I get the output as 7.
) It looks to be dependent on 't'.
) If i change the value of t as 5, the output is 5.
)
) How does it give 7 as ouput ? Any reasons for
) the dependency on 't' variable ?
) Does C support this kind of assignment of x=(r,s,t);
) for any specific reasons ?

What do you mean by 'this kind of assignment' ?
It's a perfectly normal assignment.

What you may be confused by is the value of the expression (r,s,t).

Try this:

printf("(r,s,t) values is $d\n",(r,s,t));

And then look up the 'comma operator' in your C book.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
B

Bartc

karthikbalaguru said:
Hi,

I get a strange output with the below code snippet.

#include<stdio.h>
int main(void)
{
int r=2,s=3,x=0,t=7;
x=(r,s,t);

C evaluates each of r,s,t then returns the value of the last in the list, in
this case t.

In the way you've used it, this isn't very useful.

What value did you expect in x, the tuple 2,3,7? In that case you did a
different language.
 
K

karthikbalaguru

C evaluates each of r,s,t then returns the value of the last in the list, in
this case t.

Interesting !! :)
In the way you've used it, this isn't very useful.

Where will such kind of expression be useful ?
Is there any specific name for such kind of expressions ?

Thx in advans,
Karthik Balaguru . T
 
R

Richard

karthikbalaguru said:
Interesting !! :)


Where will such kind of expression be useful ?

x=x+1,x=x * 2,x

maybe?

Personally I think thats not useful and would do it more clearly.
 
J

John Bode

Hi,

I get a strange output with the below code snippet.

#include<stdio.h>
int main(void)
{
   int r=2,s=3,x=0,t=7;
   x=(r,s,t);
   printf("x values is %d \n",x);
   return 0;

}

I get the output as 7.
It looks to be dependent on 't'.
If i change the value of t as 5, the output is 5.

How does it give 7 as ouput ? Any reasons for
the dependency on 't' variable ?
Does C support this kind of assignment of x=(r,s,t);
for any specific reasons ?

Thx in advans,
Karthik Balaguru . T

The "r,s,t" expression is known as a sequential expression or a comma
expression; each operand is evaluated in order, and the value of all
but the last operand are discarded. So the statement

x=(r,s,t);

is equivalent to

r;
s;
x=t;

Personally, I've never found a practical use for it.
 
F

fmassei

The "r,s,t" expression is known as a sequential expression or a comma
expression; each operand is evaluated in order, and the value of all
but the last operand are discarded.  So the statement

    x=(r,s,t);

is equivalent to

   r;
   s;
   x=t;

Personally, I've never found a practical use for it.

It's normal to find it (and very useful) in the "for" statement:

for (i=0, j=1; i<10; ++i, ++j) {
/* the code */
}

instead of writing:

j=1;
for (i=0; i<10; ++i) {
/* the code */
++j;
}

In this case is even more visually clean to use the comma operator.
 
S

Stephen Sprunk

karthikbalaguru said:
Where will such kind of expression be useful ?

The primary case I've seen it used is in a for() loop with multiple loop
variables, e.g.:

for (int x=0, y=10; x<10; x++, y--) {
/* do something */
}

In most other cases, you could just as easily break the expression into
multiple statements, which would be clearer. However, that's not
possible in the above example because of the syntax restrictions.
Is there any specific name for such kind of expressions ?

Look up "comma operator" in your textbook.

S
 
K

Keith Thompson

John Doe said:
karthikbalaguru said:
Where will such kind of expression be useful ?

K&R 4.1, in strindex():
for (j=i, k=0; t[k] != '\0' && s[j] == t[k]; j++, k++)
;

More generally, the comma operator is useful when you need to put
multiple expressions with side effects (generally assignments) into a
context that doesn't allow multiple statements. A for loop, as seen
above, is a good example. (An if statement generally is not, because
all but the last assignment can be moved to a separate statement
preceding the if.)

Another common example is a macro definition, where an invocation of
the macro is intended to work like a function call. Since a function
call is an expression, and statements cannot appear within an
expression, you sometimes have to combine assignments with commas
rather than semicolons.

(For a macro that's intended to be invoked as a standalone expression,
not part of a larger expression, you can use statements, so the comma
operator isn't necessary -- but the "do { ... } while (0)" trick can
avoid some problems.)
 
J

Joachim Schmitz

Stephen said:
The primary case I've seen it used is in a for() loop with multiple
loop variables, e.g.:

for (int x=0, y=10; x<10; x++, y--) {

Isn't this C++ rather than C? Or did I miss that C99 introduced it?

Is the ", " above really the comma operator? AFAIR the ";" in the for() is
not a sequencw point as if used to finish a statement.

Bye, Jojo
 
H

Harald van Dijk

Isn't this C++ rather than C? Or did I miss that C99 introduced it?

Using a declaration instead of an expression in a for statement is new in
C99, but

int x, y;
for (x=0, y=10; x<10; x++, y--) {

can appear in C90 programs.
Is the ", " above really the comma operator?
Yes.

AFAIR the ";" in the for()
is not a sequencw point as if used to finish a statement.

The ; is the for statement is indeed not a statement terminator, and the
expressions are not expression statements. However, there is a sequence
point at the end of a full declarator, and at the end of a full
expression, regardless of whether they appear by themselves or inside a
for statement.
 
J

Joachim Schmitz

Harald said:
Using a declaration instead of an expression in a for statement is
new in C99, but

OK, I had the distinct feeling that I was missing something here ;-). Hadn't
yet been exposed to a C99 compiler...
int x, y;
for (x=0, y=10; x<10; x++, y--) {

can appear in C90 programs.

I knew that (of course)

OK, had I read more of the thread, I hadn't asked...
The ; is the for statement is indeed not a statement terminator, and
the expressions are not expression statements. However, there is a
sequence point at the end of a full declarator, and at the end of a
full expression, regardless of whether they appear by themselves or
inside a for statement.

Thanks for the explanation

Bye, Jojo
 
M

MisterE

And then look up the 'comma operator' in your C book.

Its interesting that the comma operator is so seldom used, even in its only
usual place in for(;;).
I once saw some code that , joined every possible line it could. Not sure
why.
 
H

Harald van Dijk

OK, had I read more of the thread, I hadn't asked...

I missed something here, sorry. The first comma above is not the comma
operator. The second comma is. There is a sequence point after the
evaluation of "0" and "10" in "int x=0, y=10", but I believe there is no
requirement that "0" is evaluated before "10".

In other words,

int a=1;
int b=++a, c=++a;

must set b and c to 2 and 3, or to 3 and 2, and there is no requirement
that the implementation is consistent in how it chooses.

Corrections welcome, because it's very well possible I'm overlooking
something here.
 
J

Joachim Schmitz

Harald said:
I missed something here, sorry. The first comma above is not the comma
operator.

But in the C89 variant,
{
int x, y;
....
for (x=0, y=10; x<10; x++, y--) {
....
the first comma (in the for) would be a comma operator, right?

Bye, Jojo
 
H

Harald van Dijk

But in the C89 variant,
{
int x, y;
...
for (x=0, y=10; x<10; x++, y--) {
...
the first comma (in the for) would be a comma operator, right?

Yes, that is correct.
 
B

Ben Bacarisse

Joachim Schmitz said:
Isn't this C++ rather than C? Or did I miss that C99 introduced it?

Answered, so I'll leave it.
Is the ", " above really the comma operator?

I don't think this has been full answered. The "above" has two
commas. The second one is a comma operator but the first one is not.
The first comma is part of the syntax of a declaration. C99 has a new
form of 'for':

for ( /declaration/ /expression/<opt> ; /expression/<opt> ) /statement/

[If your news reader does not so /italics/ this will look odd.
Sorry.]
 
J

Joachim Schmitz

Ben said:
Answered, so I'll leave it.


I don't think this has been full answered. The "above" has two
commas. The second one is a comma operator but the first one is not.

Harald did clarify this meanwhile.
The first comma is part of the syntax of a declaration. C99 has a new
form of 'for':

for ( /declaration/ /expression/<opt> ; /expression/<opt> )
/statement/

[If your news reader does not so /italics/ this will look odd.
Sorry.]

Which newsreader does?

Bye, Jojo
 
B

Ben Bacarisse

Harald van Dijk said:
I missed something here, sorry. The first comma above is not the comma
operator. The second comma is.

[I chipped in with the same point when I thought the thread had left
the topic. I should have just read to the end.]
There is a sequence point after the
evaluation of "0" and "10" in "int x=0, y=10", but I believe there is no
requirement that "0" is evaluated before "10".

In other words,

int a=1;
int b=++a, c=++a;

must set b and c to 2 and 3, or to 3 and 2, and there is no requirement
that the implementation is consistent in how it chooses.

Corrections welcome, because it's very well possible I'm overlooking
something here.

I can't be sure enough to offer a correction, but I had always assumed
that the sequence point at the end of a full declarator was to permit
(in a meaningful way):

int x = 1, y = x + 1;

If so, then your example must give b == 2 and c == 3.
 

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,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top