Explain solution ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Explain solution ?

Which increment take place first ? #include <stdio.h> int main() { int i =4; printf("print %d%d %d%d\n",i++,++i,++i,i--); printf("%d",i); return 0; } https://code.sololearn.com/cc1WXkoctfQ0/?ref=app

8th Dec 2018, 6:40 AM
Vashisth Malik
Vashisth Malik - avatar
16 Answers
+ 9
Hmm... I dont know that im true or not, but they will count from the right for i++, and the final value for ++i step: i = 4 i++, ++i, ++i, i-- i++, ++i, ++i, 4 -> i = 3 i++, ++i, i, 4 -> i = 4 i++, i, i, 4 -> i = 5 5, i, i, 4 -> i = 6 5, 6, 6, 4
8th Dec 2018, 6:57 AM
ShortCode
+ 21
[ edit : might precedence & associativity are not playing any role , still can have a look at these points for later use ] ● Precedence of postfix x++ is more than prefix ++x.[same for decrement operator] ● Associativity of postfix x++ is left to right. ●Associativity of prefix ++x is right to left. //try this example for better understanding : int main() { int i =4; printf("%d %d %d %d %d\n",i++,i++,++i,++i,i--); // start from right most part printf("%d",i); return 0; } Source for more information(go for typical case) : http://www.c4learn.com/c-programming/increment-operator-inside-printf/
8th Dec 2018, 7:09 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
Yes Kishalaya Saha is correct in that , might here is violation of expression evaluation rules in C . Example : int i =4; printf("%d %d\n",i++,i++); // 5 4 [started evaluation from right, in place of left] printf("%d",i); //6
8th Dec 2018, 8:48 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
I wonder why digesting the concept of UB is so hard for most folks, here! https://en.wikipedia.org/wiki/Undefined_behavior
8th Dec 2018, 2:03 PM
Babak
Babak - avatar
+ 7
First, neither precedence nor associativity explains what's happening here. And I think ShortCode's explanation is better than the one given in the link. The link doesn't mention that the values of the i-s in the middle are its final value (unless I understood it wrong). But bear in mind that this is undefined behaviour, so the output could be different on a different compiler, and it's best not to do this anyway. Coincidentally, I came up with pretty much the same explanation as ShortCode on a similar post (see HonFu's comment). You can also read more about undefined behaviour there: https://www.sololearn.com/Discuss/1609437/?ref=app
8th Dec 2018, 8:35 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 6
Gaurav Agrawal said, "might here is violation of expression evaluation rules in C ." For what it's worth, I don't think it's a violation of any rule. It's undefined behaviour, like Babak and I said.
8th Dec 2018, 2:18 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 6
Kishalaya Saha No i haven't..sure I'll go through it 😊
10th Dec 2018, 5:06 PM
Sia
Sia - avatar
+ 4
print 56 64 6 This is incorrect output guys. The output will be print 55 44 6 Explanation:- The counting will be frm right to left. i=4 i++ , ++i , ++i , i-- i++ , ++i , ++i , 4 ..... i=3 i++ , ++i , 4,4 .... i=4 i++ , 5 , 4 , 4 ..... i=5 5 , 5 , 4 , 4 ...i=6 and finally the second line printf("%d",i); will give 6
9th Dec 2018, 10:17 AM
Sia
Sia - avatar
+ 4
Kishalaya Saha I have checked the code in the world famous Turbo compiler. It also gives Output 55 44 6. There's definitely something wrong with Sololearn compiler. Incorrect is incorrect. This kind of compiler behavior that too in such a sensitive stuff.. it's disappointing!
10th Dec 2018, 4:59 PM
Sia
Sia - avatar
+ 2
Kishalaya Saha I see you having tough time to beat the concept into their head! 8(
10th Dec 2018, 6:13 PM
Babak
Babak - avatar
+ 1
Shriya Shekhar this is undefined behaviour, so there's no "incorrect" output. Maybe in your compiler it prints 55 44, but on Sololearn's compiler it prints 56 64. Feel free to run the code linked in the main post.
9th Dec 2018, 10:25 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Shriya Shekhar There's nothing wrong with Sololearn's compiler, at least, when it comes to this specific question. Have you read about undefined behavior in the link Babak shared? If not, please do!
10th Dec 2018, 5:04 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Awesome! Now we have proof of three different compilers producing three different outputs! It only highlights the fact that this is undefined behaviour! Akhilesh Achary , I suggest you to please read about it too! See the Wikipedia article Babak linked, and also his the comments in the post here (most of which are by Babak as well): https://www.sololearn.com/Discuss/1609437/?ref=app
11th Dec 2018, 2:55 AM
Kishalaya Saha
Kishalaya Saha - avatar
0
The Answer Will be: 46 77 6
10th Dec 2018, 7:11 AM
PRAMOD HP
0
46 77 6 because as we start with left and move towards right first the value i++(5)but prints 4 as it is a post increment,then ++i(6)also prints 6 as it is a pre increment..++i(7) prints 7... then i--(6) but prints 7... Last one prints 6 as the value changed to 6.
10th Dec 2018, 5:23 PM
PRAMOD HP