freelanceprogrammers.org Forum Index » C
O/P
Joined: 25 Jul 2003
Posts: 1
O/P
int a=10;printf("%d,%d,%d",a,a--,a++);
O/P will be 10 10 9
Joined: 28 Jul 2003
Posts: 3
O/P
Hi friends
For past few threads, our point of consideration is on
pre/post incrementation/decrementing, which made me post this message.
HTH
I consider the post/pre operators to be evil. In solving
any problem regarding post/pre operators the rule of the thumb are
1) In a given expression the value of a variable could change only once.
2) Assignment of values of a variable to any operator or function in any
compiler follows stack, that is to say from right to left.
Keeping the above rules in mind lets solve some equation`s
int i = 10;
printf("The output is [%d]
",i-- * i++);
In the above statement there is an expression which reads ( i-- * i++ ).
In this expression there is only one variable and according to the rule
the value could change only once, following the 2nd rule the assignment
starts from right, which means that the operator "*" will receive first
argument as 10 since it is post incrementation. Now the value of "i"
can`t change in the same expression so the first operand to operator "*"
will also be 10; the expression becomes
printf("The output is [%d]
",10 * 10);
the output will be :-
*************** The output is [100] **************
Now reversing the scenario let the statement be:-
int i = 10;
printf("The output is [%d]
",i-- * ++i);
In this case the output will be :-
*************** The output is [121] **************
because the compiler will evaluate the expression as follows :-
printf("The output is [%d]
",11 * 11);
since first it encountered "++i" which is a pre incrementation. After
the execution of the "printf" statement the value of the variable will
be "10". This is because the compiler does not forget the operation that
it has to carried out ei:- "i--";
It only postpones the execution for later.
Now turning our point from evaluation of expression to assignment of values;
Consider this statement :-
int i = 10;
printf("%d,%d,%d,%d",i++,++i,i--,--i);
The output of the above statement will be :-
************************ 9,9,9,9 *********************************
let`s understand why who step by step;
1) The thumb rule says assignment starts from right,which makes the
last "%d" the first recipient which is "--i" which evaluates to 9.
In this assignment the operation on the variable was carried out
first because it was pre decrementation.
2) Then comes the last but one "%d", in which the value will be 9 because
the variable is post decremented, now after the assignment the
execution is still to perform subtracting which makes the value
of the variable "8".
THE TRICK IS :- FIRST THE ASSIGNMENT TOOK PLACE AS "9" AND THEN THE
REMAINING OPERATION WAS PERFORMED ON THE VARIABLE.
3) Then comes the assignment "++i" which assigns the value "9" to the
associated "%d". The reason being that, the previous value of
"i" was "8" as we saw in the step 2, and "i" being pre incremented
first increments the value which make it "9" or "8+1".
This also goes true for the first "i++" expression.
Note : - Do not use post incrementation or decrementation
in recurssion.
I hope that this is enough to clear any doubt, and hope that it helps all
:-
Happy Coding :-)
S.Navin
Phone : 0712-3118509
Burn the Author [navinseshadri@...]
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
Joined: 19 Jul 2003
Posts: 29
O/P
This`s a long post I am replying to, so decided to
insert my text here in the beginning.
Parameters are procesed from the stack alright, but
when we talk about expressions the focus shifts to a
binary-tree (stack or stackless) representation of it.
And we already know, there are three ways of
traversing a binary tree -- (1) Pre-order (2)
Post-order and (3) In-order. So, probably it`s not
possible to anticipate implementations flatly just
because they work off the stack.
Do tell if you find my reasoning wrong, or in case you
have any link where they describe why what you think
is right, please tell that too.
Regards,
Shantanu
--- Navin <navinseshadri@...> wrote:
> Hi friends
> For past few threads, our point of consideration
> is on
> pre/post incrementation/decrementing, which made me
> post this message.
> HTH
> I consider the post/pre operators to be evil. In
> solving
> any problem regarding post/pre operators the rule of
> the thumb are
>
> 1) In a given expression the value of a variable
> could change only once.
> 2) Assignment of values of a variable to any
> operator or function in any
> compiler follows stack, that is to say from right
> to left.
>
> Keeping the above rules in mind lets solve some
> equation`s
>
> int i = 10;
> printf("The output is [%d]
",i-- * i++);
>
> In the above statement there is an expression which
> reads ( i-- * i++ ).
> In this expression there is only one variable and
> according to the rule
> the value could change only once, following the 2nd
> rule the assignment
> starts from right, which means that the operator "*"
> will receive first
> argument as 10 since it is post incrementation. Now
> the value of "i"
> can`t change in the same expression so the first
> operand to operator "*"
> will also be 10; the expression becomes
>
> printf("The output is [%d]
",10 * 10);
>
> the output will be :-
>
> *************** The output is [100] **************
>
> Now reversing the scenario let the statement be:-
>
> int i = 10;
> printf("The output is [%d]
",i-- * ++i);
>
> In this case the output will be :-
>
> *************** The output is [121] **************
>
> because the compiler will evaluate the expression as
> follows :-
>
> printf("The output is [%d]
",11 * 11);
>
> since first it encountered "++i" which is a pre
> incrementation. After
> the execution of the "printf" statement the value of
> the variable will
> be "10". This is because the compiler does not
> forget the operation that
> it has to carried out ei:- "i--";
> It only postpones the execution for later.
>
> Now turning our point from evaluation of expression
> to assignment of values;
> Consider this statement :-
>
> int i = 10;
> printf("%d,%d,%d,%d",i++,++i,i--,--i);
>
> The output of the above statement will be :-
> ************************ 9,9,9,9
> *********************************
> let`s understand why & who step by step;
>
> 1) The thumb rule says assignment starts from
> right,which makes the
> last "%d" the first recipient which is "--i"
> which evaluates to 9.
> In this assignment the operation on the variable
> was carried out
> first because it was pre decrementation.
> 2) Then comes the last but one "%d", in which the
> value will be 9 because
> the variable is post decremented, now after the
> assignment the
> execution is still to perform subtracting which
> makes the value
> of the variable "8".
> THE TRICK IS :- FIRST THE ASSIGNMENT TOOK PLACE
> AS "9" AND THEN THE
> REMAINING OPERATION WAS PERFORMED ON THE
> VARIABLE.
>
> 3) Then comes the assignment "++i" which assigns the
> value "9" to the
> associated "%d". The reason being that, the
> previous value of
> "i" was "8" as we saw in the step 2, and "i"
> being pre incremented
> first increments the value which make it "9" or
> "8+1".
>
> This also goes true for the first "i++" expression.
> Note : - Do not use post incrementation or
> decrementationin recurssion.
> I hope that this is enough to clear any doubt, and
> hope that it helps all:-
> Happy Coding :-)
> S.Navin
> Phone : 0712-3118509
> Burn the Author [navinseshadri@...]
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site
> design software
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
Joined: 25 Jul 2003
Posts: 48
O/P
See reply embedded...
-----Original Message-----From: Navin [mailto:navinseshadri@...] Sent: Monday, July 28, 2003 00:23To: Programmers-Town@yahoogroups.comSubject: *(&Programmers-Town&)* Pre/Post the Devil
Hi friends For past few threads, our point of consideration is onpre/post incrementation/decrementing, which made me post this message.HTH I consider the post/pre operators to be evil. In solvingany problem regarding post/pre operators the rule of the thumb are
What `rule` are you talking about here? Are you talking about coding standard or language specification?
1) In a given expression the value of a variable could change only once.
How about this:
a[i] = i++;
`i` only changed once, but the expression still exhibit undefined behavior.
2) Assignment of values of a variable to any operator
What do you meant by this? What does assignment has to do with stack?
If you`re talking about associativity, note that associativity and order of evaluation are two different things. See:
<http://www.eskimo.com/~scs/C-faq/q3.4.html>
or function in any compiler follows stack, that is to say from right to left.
I don`t think the language specification specified push order or stack. Those are implementation specific. The order of argument passing is controlled by calling convention. And this has nothing to do with order of evaluation of function argument.
The language required that the arguments to a function be completely evaluated and all side-effects settled before entering the function. The implementation is free to evaluate the arguments in any order. Only after the arguments are evaluated, calling convention determine the order of argument passing (right to left, left to right, register, etc...).
Keeping the above rules in mind lets solve some equation`sint i = 10;printf("The output is [%d]
",i-- * i++);In the above statement there is an expression which reads ( i-- * i++ ).In this expression there is only one variable and according to the rulethe value could change only once,
Again, what rule? Do you mean that the language specification request that the compiler should only change the value of `i` once? And if so, what is the rule to determine which changes (increment, decrement) should take place? The right operand because the "push order" is right-to-left? or the left operand because multiplication is left-to-right associativity?
following the 2nd rule the assignmentstarts from right, which means that the operator "*" will receive firstargument as 10 since it is post incrementation. Now the value of "i"can`t change in the same expression so the first operand to operator "*"
will also be 10; the expression becomes
Where in the language specification you see that prevent the post-decrement taking place? Do you expect the compiler to modified the code to that extent?
[snipped]
Now turning our point from evaluation of expression to assignment of values;Consider this statement :-int i = 10;printf("%d,%d,%d,%d",i++,++i,i--,--i);
The only assignment I see is:
i = 10;
What makes you think that printf() "assign" values to format specifier?
[snipped]
I would suggest you read up some programming books and make sure you understand the concept. Understand what is a sequence point, what constitute an undefined behavior. Learning a language by assuming certain behavior exhibit by a complier/platform and take that as a rule is at best dangerous. And it will takes you more time to unlearn the mistakes.
Shyan
Joined: 28 Jul 2003
Posts: 3
O/P
Well I don`t have any link with me to support my point
nither do I have any link to describing the behaviour.
You were right about the expression but I wount aggree
with you on that note. I understand that "C" compiler
parse an expression and convert it into (Post fix
notation ) which is nothing but reverting the senario.
--- Shantanu Kumar <shantanu_k06@...> wrote:
> This`s a long post I am replying to, so decided to
> insert my text here in the beginning.
>
> Parameters are procesed from the stack alright, but
> when we talk about expressions the focus shifts to a
> binary-tree (stack or stackless) representation of
> it.
> And we already know, there are three ways of
> traversing a binary tree -- (1) Pre-order (2)
> Post-order and (3) In-order. So, probably it`s not
> possible to anticipate implementations flatly just
> because they work off the stack.
>
> Do tell if you find my reasoning wrong, or in case
> you
> have any link where they describe why what you think
> is right, please tell that too.
>
> Regards,
> Shantanu
=====
Bye for now
Navin
navinseshadri@...
0712-3118509
bye :)
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
Joined: 26 Jul 2003
Posts: 20
O/P
On Mon, 28 Jul 2003 08:51:37 -0700 (PDT), Shantanu Kumar
<shantanu_k06@...> wrote:
I just happened to notice this thread. Well., the discussion will lead
to nothing. Coz, the order of evaluation of arguments to a function call
is not defined by C99. This is what C99 says.,
-- quote --
C99:-6.5.2.2-10
The order of evaluation of the function designator, the actual
arguments, and subexpressions within the actual arguments is
unspecified, but there is a sequence point before the actual call.
-- unquote --
Further, this is clearly said as an example for *Unspecified
characteristics* in 3.4.4
-- quote --
C99:-3.4.4-2
EXAMPLE An example of unspecified behaviour is the order in which the
arguments to a function are evaluated.
-- unquote --
So, C99 guarantees that there will be *only a sequence
point* before the actual call., and it is clearly stated that the order
in which the evaluation of arguments occurs is undefined. Therefore
discussing on a non-standard issue will not be of any help to anybody.
Further it might mis-lead new programmers into a non-constructive
approach to Computing.
I hope the members will refrain from raising non-standard issues
such as these on the group.
Cheers,
Joe
--
"Software is like sex; Its better when it is free"
-- Linus Torvalds
"The kingdom of heaven is spread out upon the earth,
and men do not see it" --Jesus, Gospel of Thomas 113
Visit:http://www.joesteeve.tk/
PS:> I hate HTML mail
Joined: 08 Jul 2003
Posts: 6
O/P
On Fri July 25 2003 18:35, Mishra Vaibhav wrote:
> int a=10;
> printf("%d,%d,%d",a,a--,a++);
>
>
> O/P will be 10 10 9
hi all
this is a long thread
in gcc3.2 the precdence is as follows
it`s right to left and the operator is evaluated after , not after ; as in
some compilers.
-suman
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







