0

I am trying to run this code.this code runs successfully.But when i change variables to constant numbers i get compilation error.

My code that works fine:

 int a =5,b=6,c=7;
 int d;
 d = a,b,c;
 printf("%d",d);

as i run the code its output is 5. but when i run this segment of code:

 d = 2,6,7;
 printf("%d",d);

i get compilation error.I tried i on other compiler also. But the error still exists. What i am doing wrong.

Rahul
  • 5,594
  • 7
  • 38
  • 92

1 Answers1

3

Your first code use the variables and assignment d = a and b and c just as expression there, so run the code:

int main(int argc, char const *argv[])
{
 int a =5,b=5,c=7;
 int d;
 d = a,b,c+1;
 printf("%d",d);
 return 0;
}

You get 5, b and c+1 just valued and put them there useless.But if you run this code which includes comma expression:

int main(int argc, char const *argv[])
{
 int a =5,b=5,c=7;
 int d;
 d = (a,b,c+1);
 printf("%d",d);
 return 0;
}

You get 8 as the last one valued expression. You can use the number play as an expression with ():

int main(int argc, char const *argv[])
{
 int a =5,b=5,c=7;
 int d;
 d = (0,3,1);
 printf("%d",d);
 return 0;
}

get the last number or valued data.

It works for me the code below:

int main(int argc, char const *argv[])
{
 int a =5,b=5,c=7;
 int d;
 d = 0,3+1,1-1;
 printf("%d",d);
 return 0;
}

it output is 0, but if you don't with (), it meaningless by this way, why not just use d = 0;

lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30