why same code gives diffrent output on diffrent compilers ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why same code gives diffrent output on diffrent compilers ?

when I run the code on cxxdroid then program output is 0 20 1 but, when I run the code on other compiler then program output is 1 20 1 why diffrent diffrent output? what is the effect of return type on printf() function ? EDIT ➡️ : I donno why this code is not working on sololearn I tried to run with cxxdroid ! here is the code : try to change int to void https://code.sololearn.com/c5krsCkI48GH/?ref=app

5th Jul 2021, 12:56 PM
Ratnapal Shende
Ratnapal Shende - avatar
15 Answers
5th Jul 2021, 7:26 PM
Jayakrishna 🇮🇳
+ 2
Just saying additionally.. It's allowed to use assignment in print function. It's legal according syntax. #include <stdio.h> int main() { int x=10; printf("%d ",x=20); } And void main(){} are used in old c compliers. Modern compilers requires to use int main(). Otherwise it's throws warning.. So it advised " not be to use old compilers like turbo c." And also not use sequence point issue instructions or commands.
5th Jul 2021, 2:23 PM
Jayakrishna 🇮🇳
+ 1
yeah it's giving me warning but code is running... but why different output I don't understand that... please explain..
5th Jul 2021, 1:15 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
Jayakrishna🇮🇳 why diffrent outputs 0 20 1 //cxxdroid 1 20 1 //online compiler (programiz )
5th Jul 2021, 1:29 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
You can easily verify that in this case the sequence does not change from right to left by changing the code as follows: printf("%d %d %d", x != 10, x < 30, x);
5th Jul 2021, 2:14 PM
Solo
Solo - avatar
+ 1
I'm sorry, I don't know English well and I use a translator. I wanted to say that in conjunction with the logical operator, the assignment operator cannot be used, since the interpreter automatically expects the next operator to be also logical. Something like this ☺️
5th Jul 2021, 2:41 PM
Solo
Solo - avatar
+ 1
Jayakrishna🇮🇳 if compilers excutes expressions right to left and left to right then what is the case here .. int main() { int x=20; printf("%d %d %d %d\n",x==20,x=10,x==10,x<30); } output in cxxdroid : 1 10 1 1 output in online compiler(programiz ) : 0 10 0 1 which is weired right ? I am still confused why the outputs are diffrent.... please explain it...
5th Jul 2021, 6:20 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 1
Ratnapal Shende your code has undefined behavior means you cannot predict output before. so it is recommended to not use that type of code in our programs. @Martin Taylor has already explain it clearly .. I cannot say more. read again once. and you can ask after if any thing is not clear.. standard not stated it to follow which order to follow. so different compilers implemented in different ways. so output may different in that sense.. you can read for info https://en.cppreference.com/w/c/language/eval_order edit: @Vasiliy No worries. just want to share it. Those are 3 individual operations so no conjunction is there. It like a varlus (or something else,not sure) operator in python print(x:=a+b)
5th Jul 2021, 6:28 PM
Jayakrishna 🇮🇳
+ 1
Ratnapal Shende It's a bad question according to me. Definitely not from a authority , may your teacher defined own... Also if you have not typo ,then 0 10 1 is answer. But still with a warning message.. because your are assigning same x=10 again. No change . Otherwise its undefined. May be result is 0 10 1 or 1 10 1. You should show this output to your teacher by executing.. May your teacher checked it only once..
5th Jul 2021, 6:57 PM
Jayakrishna 🇮🇳
0
You cannot use the assignment operator in conjunction with the boolean operator in the printf function.
5th Jul 2021, 1:13 PM
Solo
Solo - avatar
0
because of sequence point issue.. order of evaluation is complier dependent. it may left to right or right to left. so order of evaluation is like in order x!=10, next x=20, next x<30 :outputs 0 20 1 or x<30, next x=20 ,next x!=10 :outputs 1 20 1 it's depends on compiler implementation. not on standards. The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again. There is no concept of left-to-right or right-to-left evaluation in C, so compilers implementation was dev's choice. edit: Ratnapal Shende this may help you more. https://en.m.wikipedia.org/wiki/Sequence_point the threads I posted in above all have about similar issue. You can find there other sequence point issues..
5th Jul 2021, 1:32 PM
Jayakrishna 🇮🇳
0
Personally, I have never encountered using the main function main with initialization void in the C programming language, always only int, since main always returns an integer 0 if the code is executed correctly, or an error code. In any case, when changing int to void, the output remains unchanged with an error indication.
5th Jul 2021, 1:50 PM
Solo
Solo - avatar
0
Jayakrishna🇮🇳 but bro first question was asked in my test int void() { int x=10 ; printf("%d %d %d", x! =10, x=10,x<30); } options are 1) 1 10 1 2) 0 10 1 2) 0 1 10 4) 1 10 0 which one is correct 1 or 2 ?😢 why they asked tough questions in exams... cxxdroid : 0 10 1 others : 1 10 1
5th Jul 2021, 6:40 PM
Ratnapal Shende
Ratnapal Shende - avatar
0
Coz of different compile frame work
7th Jul 2021, 8:43 AM
Frank J Abel
Frank J Abel - avatar
5th Jul 2021, 1:21 PM
Jayakrishna 🇮🇳