in Programming in C retagged by
729 views
0 votes
0 votes

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 in C retagged by
729 views

4 Comments

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

Also read this :

 

1
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 

1
1

@sushmita what you have concluded from your sir 

0
0

1 Answer

0 votes
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 .

4 Comments

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
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
0
There's no source which says that it is right to left.
0
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?
0
0

Related questions

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