1
#include<stdio.h>
 int main()
 {  
       int i=7,j;
       j=(i++,++i,j*i); 
       return 0;
}

j=(i++,++i,j*i);Is this well defined ? Let me clear my doubt.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Jagan
  • 4,649
  • 19
  • 60
  • 70
  • 3
    Why do you want to do such a thing? Can't you just do `i += 2; j *= i`? – JeremyP Aug 28 '10 at 10:36
  • 1
    @JeremyP:Just for learning c-concepts :-) – Jagan Aug 28 '10 at 13:29
  • 1
    Who cares. You would never see this code in real life. It would not get through a code review as you would have to explain were the sequence points are and 80% of developers don;t know what a sequence point is. – Martin York Aug 28 '10 at 21:38
  • See what Martin says? All this obscure type of behaviour is just totally irrelevant to programming in the real World. The only thing you need to know to cover all this stuff is to be really careful about expressions with side effects. – JeremyP Aug 29 '10 at 09:07

3 Answers3

10

This expression is OK because the comma operator is a sequence point:

(i++, ++i, j*i)

However do not confuse it with the following where the comma is not acting as a sequence point:

somefunction(i++, ++i, j*i)

What about j = i++ * ++i

The multiplication operator is not a sequence point.


(Excuse me hijacking your answer)

From §3.4 of ISO 9899:1999 (C Standard):

3.4 behavior

external appearance or action

3.4.1 implementation-defined behavior

unspecified behavior where each implementation documents how the choice is made

EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right.

3.4.2 locale-specific behavior

behavior that depends on local conventions of nationality, culture, and language that each implementation documents

EXAMPLE An example of locale-specific behavior is whether the islower function returns true for characters other than the 26 lowercase Latin letters.

3.4.3 undefined behavior

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

EXAMPLE An example of undefined behavior is the behavior on integer overflow.

3.4.4 unspecified behavior

behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance

EXAMPLE An example of unspecified behavior is the order in which the arguments to a function are evaluated.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

Yes, It's well defined. sequence point

comma operator in C

Nyan
  • 2,360
  • 3
  • 25
  • 38
  • 2
    Undefined. There're no sequence points inside the expression and `i` is modified twice. – aib Aug 28 '10 at 11:27
1

In your code " ," will be work as sequence point.

so in this

j=(i++,++i,j*i);

expression would be work from left to right.
so at first i++ then ++i and then j*i

at the last j*i would be stored in j;

but lastly your result would be elegant because " j " have no predefined data
so undefined value would be stored in j.

if you don't use " () "

your code would be work as single statement such as

j=i++;
++i;
j*i;
userBI
  • 345
  • 1
  • 5
  • 15