Re: ANSI C syntax ?



On Mar 11, 12:10 am, Zhou Yan <zhouyan1...@xxxxxxxxx> wrote:
"SRR" <SRRajesh1...@xxxxxxxxx> writes:
This explanation will not be correct in this respect.
In C assignment is an expression and therefore evaluate to the
assigned value.
But statements are not expressions!!
In the code given in the question, a compound statement is enclosed
within paranthesis and is "assigned" to an int variable!
Note that a statement is just a statement and does not yield any
value!!

So, I see that I am wrong...but could you please kindly explain why
that code will produce the out put "a = 4", I managed to compile that
code fragment and really got "a = 4", which make me think about that
the right oprand {...} yield a value 2.
If it is just

a = ( c = 4 );

that is clearly what happend. But does that code

a = ( { int c; c = 2 + a; } );

also give us a = 4?

Thanks

I think that, the result is a consequence of the way the compiler
parses the tokens and compiles them.

You can't use the standards of C to explain the result you get because
I had already said a statement does not yield a vlaue as per its very
definition.

As per the C standards for the assignment there are following
constraints:

One of the following shall hold:94)
- the left operand has qualified or unqualified arithmetic type and
the right has
arithmetic type;
- the left operand has a qualified or unqualified version of a
structure or union type
compatible with the type of the right;
- both operands are pointers to qualified or unqualified versions of
compatible types,
and the type pointed to by the left has all the qualifiers of the type
pointed to by the
right;
- one operand is a pointer to an object or incomplete type and the
other is a pointer to a
qualified or unqualified version of void, and the type pointed to by
the left has all
the qualifiers of the type pointed to by the right;
- the left operand is a pointer and the right is a null pointer
constant; or
- the left operand has type _Bool and the right is a pointer.

Since none of the above constraints are satisfied by the given
statement, it is a constraint violation and you can't use C standards
to explain the result. I guess an error diagnostic is required by any
ANSI compiler, as the constraint is violated.

Correct me if I'm wrong.


.



Relevant Pages

  • Re: C Standard Regarding Null Pointer Dereferencing
    ... If the operand points to a ... Where is a null pointer ... an assignment or a comparison. ...
    (comp.lang.c)
  • C Standard Regarding Null Pointer Dereferencing
    ... "If the operand has type 'pointer to type', ... But compare with and, which do describe evaluation, albeit ... There is no assignment here. ...
    (comp.lang.c)
  • Re: calculating length of an substring
    ... pointer from a pointer. ... arithmetic type and the right has arithmetic type; ... In simple assignment, ... operand is converted to the type of the assignment ...
    (comp.lang.c)
  • Re: Is `removal of const qualifier from target pointer a warning or an error?
    ... a.c:4: warning: initialization discards qualifiers from pointer target ... `const char' and `char' are not qualified ... The relevant constraint is 6.5.16.1p1, which requires that for simple assignment "both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;" In other words, you cannot discard qualifiers from the pointed-at type during assignment, but you can add them. ...
    (comp.lang.c)
  • Re: Some pointer quiestions again
    ... int main ... type of the pointer value "i" is "int", which is not compatible with "unsigned char". ... The only constraint for the assignment operator I found is "An assignmen operator shall have a modifiable lvalue as its lef operand." ... by the left has all the qualifiers of the type pointed ...
    (comp.lang.c)