6
int main()
{
    int a = (1,2,3);
    int b = (++a, ++a, ++a);
    int c= (b++, b++, b++);
    printf("%d %d %d", a,b,c);
}

I am beginner in programming. I am not getting how does this program shows me output of 6 9 8.

artm
  • 17,291
  • 6
  • 38
  • 54
sagar
  • 379
  • 4
  • 13
  • 1
    This is what's known as "obfuscated" code, i.e., code which is deliberately written to confuse people. Although it can sometimes illustrate obscure aspects of a language, it is not recommended for beginners as it is likely to teach them bad habits, and in most cases they won't be able to figure it out on their own. – Tom Karzes Dec 08 '15 at 11:40
  • I'm voting to close this question as off-topic because obfuscated trash code that is of zero or negative use to future users/visitors. – Martin James Dec 08 '15 at 11:53
  • 1
    @MartinJames; I do not think that this code has zero or negative use. – haccks Dec 08 '15 at 11:59
  • ^^^ OK, then we disagree:) – Martin James Dec 08 '15 at 12:16
  • Classmate? http://stackoverflow.com/questions/34155644/execution-of-parameters-in-printf-function-in-c-programming-language – Mogsdad Dec 08 '15 at 19:06

3 Answers3

8

The , used in all the three declarations

int a = (1,2,3);
int b = (++a, ++a, ++a);
int c = (b++, b++, b++);  

are comma operator. It evaluates the first operand1 and discard it, then evaluates the second operand and return its value. Therefore,

int a = ((1,2), 3);          // a is initialized with 3.
int b = ((++a, ++a), ++a);   // b is initialized with 4+1+1 = 6. 
                             // a is 6 by the end of the statement
int c = ((b++, b++), b++);   // c is initialized with 6+1+1 = 8
                             // b is 9 by the end of the statement.

1 Order of evaluation is guaranteed from left to right in case of comma operator.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • " It evaluates the first operand and discard it, then evaluates the second operand and return its value." -- It evaluates all the operands and returns the last one: it doesn't stop at the second one if there is a third one. – nicomp Dec 08 '15 at 11:43
  • @nicomp; There is no third operand! Its a binary operator. – haccks Dec 08 '15 at 11:43
  • (b++, b++, b++); is three operands but I see what you mean now. It is a series of two-operand operations. My bad. – nicomp Dec 08 '15 at 11:49
  • 1
    @nicomp It is actually `((b++,b++),b++)` so `(b++,b++)` is considered as one operand to outer comma `,` . – ameyCU Dec 08 '15 at 11:50
5

The code is not in any way good and nobody in their right mind would ever write it. You should not spend any time in looking at that kind of code, but I will still give an explanation.

The comma operator , means "do the left one, discard any result, do the right one and return the result. Putting the parts in parentheses doesn't have any effect on the functionality.

Written more clearly the code would be:

int a, b, c;

a = 3; // 1 and 2 have no meaning whatsoever

a++;
a++;
a++;
b = a;

b++;
b++;
c = b;
b++;

The pre- and post-increment operators have a difference in how they act and that causes the difference in values of b and c.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • 2
    It's bad code but you *should* spend time looking at it because it will make you a better coder. It's good to be inquisitive and learn what not to do. It's the same reason athletes drag tires around and wear weighted vests when they work out: they work harder in practice than in the real game. – nicomp Dec 08 '15 at 11:37
  • 2
    Why did you use postincrement in your code snippet? – Armen Tsirunyan Dec 08 '15 at 11:39
  • @ArmenTsirunyan Just a habit of using it normally and since in this form the post- and pre-increments don't matter, it comes naturally. They could be in pre-increment also. – Sami Kuhmonen Dec 08 '15 at 11:54
  • @nicomp True, maybe should've said one shouldn't go out to seek this kind of code purposefully. – Sami Kuhmonen Dec 08 '15 at 11:55
  • 1
    @ Sami Kuhmonen seeking out obfuscated code is fun and educational. There's an international obfuscated C Code contest every year. http://ioccc.org/ – nicomp Dec 08 '15 at 11:56
  • @nicomp Yes, but that especially is not for beginners :) – Sami Kuhmonen Dec 08 '15 at 11:57
  • @nicomp Most people aren't athletes. *Some* athletes drag tires around and wear weighted vests when they work out. *Some* programmers find the IOCCC to be fun and educational. But most programmers just get confused by this stuff (just as most people would hurt themselves if they dragged tires around or wore weighted vests). In particular: a beginning programmer "learns" from this thread that he can predict the behavior of a single expression involving multiple instances of `++a`, and that is a terribly, terribly wrong and dangerous lesson to learn. – Steve Summit Sep 03 '17 at 15:12
  • @SteveSummit *Nothing* in programming is dangerous to learn. I teach the good and the bad. I demonstrate bad code and good code. In fact, next week I am teaching gotos in my C# class. I will teach the students not to do it. I will also demonstrate why they shouldn't do it. I will also give them an assignment wherein they must decipher a method I wrote that is replete with gotos. My students are in no danger whatsoever. – nicomp Sep 03 '17 at 18:55
1

I am beginner in programming. I am not getting how does this program shows me output of

Just understand comma operators and prefix ,postfix .

according to rules mentioned in links given to you

int a = (1,2,3);          // a is initialized with 3 last argument .
int b = (++a, ++a, ++a);  // a is incremented three time so becomes 6 and b initilized with 6 . 
int c = (b++, b++, b++);  // b incremented two times becomes 8  and c initialized with  8.
                          // b incremented once more time becomes 9
Community
  • 1
  • 1
Mohan
  • 1,871
  • 21
  • 34