Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: a "not so problematic" bug...



On 10 Nov 2003 02:08:53 -0800, [EMAIL PROTECTED] (Frederic BISSON)
wrote:

>Hi there !
>
>Just try
>   int i;
>   i=1+3+4,+5,+6;
>
>it doesn't generate any error message while compiling but it doesn't
>compute 1+3+4+5+6, only 1+3+4.
>
>Is it an error ? An undocumented feature ? A thing that I didn't know
>about C ?

The last.

You've run into the comma operator, which isn't used a lot, and IME
causes more confusion than it's worth.  Basically, the comma operator
executes the expression on its left, then executes the expression on
its right, and returns the value of the expression on the right.  To
extend your example above a little:

   int i,j;

   j = (i=1+3+4,+5,+6);

is equivalent to 

   j = (((i=1+3+4),+5),+6)

 and sets i to 8 and j to 6.

It lets you get by with confusing code like

   int i,j,k;

   if (some_condition) 
      i=5,j=6,k=i+j;

No braces required (!) IMHO, the only reasonable place to use the
comma operator is in the initialization clause of a for statement,
e.g.

   int sum, i, a[10];

   for (i=0, sum=0; i<10; i++) 
      sum += a[i];

HTH,

                               -=Dave
-- 
Change is inevitable, progress is not.



<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.