Re: Valid C syntax.
- From: btrower@xxxxxxxxx
- Date: Sat, 22 Nov 2008 12:45:02 -0800 (PST)
On Nov 21, 6:05 am, paresh <pareshvarsh...@xxxxxxxxx> wrote:
Is this the valid C statement.
int a,b,c;
c = 5;
<<<
a = b = c;
Can anyone throw the light on this.
-Paresh
As mentioned above, the statement is valid. It also does what you seem
to be expecting.
One thing that nobody has mentioned is whether or not you should use
such a construction. It depends. In an instance where you are setting
a bunch of variables to the same thing as a starting point, it *can*
be a good practice. Practiced C programmers will find it easy to read
and understand something like this:
{
int a, b, c, d, e, f, g, h;
a = b = c = d = e = f = g = h = 5;
}
You should not over-think what the compiler will do. Unless you are
doing something simple like the above, it is better to explicitly say
what you mean. Rather than:
a = b = c;
or
a = (b = c);
You should clearly 'say what you mean'. Not all compilers are faithful
to specifications. There is no need to expose yourself to obscure bugs
in a compiler or to errors in subsequent ports to different
languages.
In practice, I would accomplish the above in two statements if there
is any doubt as to types, side-effects, etc:
b = c;
a = b;
OR (depending upon what you are doing and how code is likely to
change)
b = c;
a = c;
I am not sure why you ask the question, but the fact you ask it at all
tells me you should likely be keeping it dead simple and doing
assignments separately as you mean them to be done.
.
- Follow-Ups:
- Re: Valid C syntax.
- From: paresh
- Re: Valid C syntax.
- References:
- Valid C syntax.
- From: paresh
- Valid C syntax.
- Prev by Date: Re: slurping in binary data
- Next by Date: Re: Size of a process
- Previous by thread: Re: Valid C syntax.
- Next by thread: Re: Valid C syntax.
- Index(es):
Relevant Pages
|