Re: Valid C syntax.



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.



.



Relevant Pages

  • Using Get() and Set() instead of accessing the variable directly
    ... I feel as though accessing variables directly is just bad practice. ... I can access private/protected variables, but when I divide up the class ... int m_x; ... I don't know if the compiler treats it as accessing the variable directly or ...
    (microsoft.public.vc.mfc)
  • Re: Does casting lvalue lead to Undefined Behaviour ?
    ... int main ... unsigned char buff; ... and in this case the problem does occur in practice. ... My compiler does not produce any diagnostics for the above ...
    (comp.lang.c)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)
  • Re: OT: Re: Perl Peeves
    ... when I see the result of a test being used as an int. ... compiler just assumed you knew what you were doing and would ... This didn't mean bool was special, declaring it just signaled to the ... What "normalization of bool results is built into the compiler"? ...
    (comp.lang.perl.misc)