The Gateway to Computer Science Excellence
0 votes
98 views

What is the output of this program?


int main(void)

{
int a = 10, b = 20, c = 30;
printf(" %d..%d..%d ", a+b+c, (b = b*2), (c = c*2));
return 0;
}
(A) 60..40..60
(B) 110..40..60
(C) 110..20..30
(D) 60..20..30

does sequence point comes into picture here?

in Programming by Boss (16.9k points)
retagged by | 98 views
0
Even I am a bit confused on this. You might have seen my comment on the forum. It would be better to ask Sir directly to clarify the doubt
0
yes i have messaged sir regarding this. let him reply. I have seen ur post. i am also confused :(
0

As per my knowledge,

printf is a function ===> comma is a separator but not operator, if comma is operator it is sequence point.

i have messaged sir regarding this

is it Arjun sir?

0
Yes exactly , here comma is not an operator. So I don't think there is any confusion with sequence point over here. Moreover no variable is getting updated more than once
0
since comma is not an operator here that is why ambiguity is here na??
0
kiran sir
+1

In find it kind of similar to https://gateoverflow.in/25109/tifr2012-b-9

Also read this :

 

+1
since comma is not an operator here that is why ambiguity is here na??

yes... it is depend upon order of evaluation of parameters of function, it is also undefinded in C ===> this question lead to undefined behavior 

0

@sushmita what you have concluded from your sir 

1 Answer

0 votes
For printf() the sequence point is defined from right to left. Evaluation starts from right to left . But there is lot of debate going on regarding this at all the platforms. I have seen this type of questions even in C++,where it was cout statement. So , Just go with right to left evaluation .

I have followed this.

So , the answer will be option B) when right to left is considered the order of evaluation .
by Active (1.2k points)
0
thanks...

Just go with right to left evaluation. Any source for this??

In general order of evaluation of function arguments is not defined right?
0

 

Generally when we pass arguments to the function they are just the value and not the expressions.

If we pass the expressions then we will known the difference in the order of evalutation.

Here is an example where the order of evaluation matters.

 


#include <stdio.h>
void function(int a ,int b, int c)
{
    printf("%d..%d..%d",a,b,c);
}

int main()
{
    int a=10;
    int b=20;
    int c=30;
    function(a+b+c,(b=b*10),(c=c*10));
   
}

the O/p of this is: 510..200..300

here also the order of evaluation of the expression is from right to left.

 

 

 

0
There's no source which says that it is right to left.
0
@Sushmita maam I think that order of evaluation of function arguments is undefined since when a function is called with arguments, there isn't any sequence point defined since the comma in between the arguments acts as a separator and not an operator. Why is there so much confusion regarding this?
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,654 questions
56,166 answers
193,872 comments
94,261 users