comma operator(,)
comma operator(,)helps us to separate variables when they are declared.
int a,b,c;
c=(a=10);
printf(“c=%d\n”,c);
c=(a=10,a+4);
printf(“c=%d\n”,c);
c=(a=5,a+6,a+20);
printf(“c=%d\n”,c);
c=(a=20,a+10,a+6,a-3);
printf(“c=%d\n”,c);
c=(a=3,a+10,a-5,a+2,a-7);
printf(“c=%d\n”,c);
c=(a=40,a+5,a-7,a+20,a-3);
printf(“c=%d\n”,c);
Example:1
/* comma_1.cpp */ #include<stdio.h> int main() { int a,c; c=(a=10); printf("1. c = %d\n",c); c=(a=10,a+20); printf("2. c = %d\n",c); c=(a=10,a+20,a-4); printf("3. c = %d\n",c); c=(a=10,a+20,a+45,a+2); printf("4. c = %d\n",c); c=(a=10,a+20,a+34,a-7); printf("5. c = %d\n",c); c=(a=10,a+20,a); printf("6. c = %d\n",c); return(0); }
/* Output */ 1. c = 10 2. c = 30 3. c = 6 4. c = 12 5. c = 3 6. c = 10
Example:2
/* comma_2.cpp */ #include<stdio.h> int main() { int a,b,c,d; d=(a=10,b=20,c=30); printf("1. d = %d\n",d); d=a=10,b=20,c=30; printf("2. d = %d\n",d); return(0); }
1. d = 30 2. d = 10