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
15 ответов
0
Ratnapal Shende
see in this link, 2nd example is same as your doubt.
Hope it helps...
https://stackoverflow.com/questions/50532818/how-to-understand-the-behaviour-of-printf-statement-in-c
+ 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.
+ 1
yeah it's giving me warning but code is running...
but why different output I don't understand that... please explain..
+ 1
Jayakrishna🇮🇳 why diffrent outputs
0 20 1 //cxxdroid
1 20 1 //online compiler (programiz )
+ 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);
+ 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 ☺️
+ 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...
+ 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)
+ 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..
0
You cannot use the assignment operator in conjunction with the boolean operator in the printf function.
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..
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.
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
0
Coz of different compile frame work
- 2
because of sequence point issue.
not same but similar topics. these may help you for more.
https://www.sololearn.com/discuss/2238290/?ref=app
https://www.sololearn.com/discuss/1690694/?ref=app
https://www.sololearn.com/discuss/2284550/?ref=app