| | |
order of evaluation
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
in our college we have a repeated C aptitude question... which goes like this....
and the answer given to us was 5,6,5.... when asked it was said that the compiler evaluates printf from right to left...... can somebody throw some light on how this statement gets evaluated.....
C Syntax (Toggle Plain Text)
int i=5 printf("%d %d %d", i--,i,i++)
and the answer given to us was 5,6,5.... when asked it was said that the compiler evaluates printf from right to left...... can somebody throw some light on how this statement gets evaluated.....
knowledge belongs to the world
The correct answer is that the result is undefined, or unpredictable. It depends on the compiler -- the answer by some compilers may be 5,5,5 because only the last version is stored. Yet other compilers, such as VC++ 2005 Express will give 6,5,5.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Last edited by Ancient Dragon; Jul 8th, 2007 at 11:18 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Right to left.
First variable evaluated is the i++. ( i = 5 ) 5 is assigned for the right most %d.
It gets incremented to 6 before is evaluated again. ( i = 6 ).
Next %d gets 6 assigned to it. Not incrementation here.
Last %d gets 6 because the decrement is after being assigned to be printed.
That's how I see it.
6, 6, 5
First variable evaluated is the i++. ( i = 5 ) 5 is assigned for the right most %d.
It gets incremented to 6 before is evaluated again. ( i = 6 ).
Next %d gets 6 assigned to it. Not incrementation here.
Last %d gets 6 because the decrement is after being assigned to be printed.
That's how I see it.
6, 6, 5
Last edited by Aia; Jul 8th, 2007 at 11:28 pm.
My understand in that i++ means the incrementation should be completed after the particular line of code (maybe the printf complicates things). Where ++i means to complete the statement before. So I would have thought the result should be 5, 5, 5 and then i++-- ... which is just 'i=5' before the next line of code.
EDIT: Well this got me curious, GCC gave me 6, 6, 5 as others have suggested.
EDIT: Well this got me curious, GCC gave me 6, 6, 5 as others have suggested.
Last edited by sillyboy; Jul 9th, 2007 at 12:42 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
> Only Ancient Dragon has the correct answer.
He should have stopped at the end of the first sentence IMO. Attempting to rationalise or explain any apparent results doesn't help.
> in our college we have a repeated C aptitude question... which goes like this
> and the answer given to us was 5,6,5
Find another college, or another tutor. The one you have doesn't have a clue.
No doubt your head is being filled with non-standard / implementation specific mush like how to rationalise UB for your current compiler. You're in for a lot of surprises when you pick up another compiler and find that much of what you've been taught isn't actually usable at all.
He should have stopped at the end of the first sentence IMO. Attempting to rationalise or explain any apparent results doesn't help.
> in our college we have a repeated C aptitude question... which goes like this
> and the answer given to us was 5,6,5
Find another college, or another tutor. The one you have doesn't have a clue.
No doubt your head is being filled with non-standard / implementation specific mush like how to rationalise UB for your current compiler. You're in for a lot of surprises when you pick up another compiler and find that much of what you've been taught isn't actually usable at all.
This is probably just a case of the professor trying to scare kids by using code in a way it generally won't be used. If your professor does insist that is the answer and won't budge, just listen to his explanation and remember his rules for the duration of the course. After you pass the course, you can go back to using code as it should be used.
•
•
Join Date: Jun 2007
Posts: 7
Reputation:
Solved Threads: 1
Hi,
Before go to perform the statement execution you should fix these two concepts
In your mind
1. Normally both increment and decrement operation’s performed in two ways. Pre- increment/post-increment and pre-decrement/post-decrement. If you declare the variable I =5 then if you want to perform I++ operation in C first the value 5 will be stored into I after assigning only it will be incremented as 6. So in next statement only I value should go to value 6.
2. The next one is precedence of the operator’s. You must consider the priority of all the operators. The precedence has the order like
High priority: * / %
Low priority: + -
Another one to understand is
For this entire operator it should give the high priority for which one statement has the ( ) operator like (operators + operands). If one statement has this entire operator in the same line then it should perform operation from left to right.
Like this only printf function should perform, so for your program you got the result like 6, 6, and 5.
Int i=5;
I++ ( I=5) // I value assigned the next line will be incremented
I (I=6) // incremented value only assigned
I—( I=6) // after assigning the value 6 then only it’ll decrement the value to 5
ans:6,6,5;
Before go to perform the statement execution you should fix these two concepts
In your mind
1. Normally both increment and decrement operation’s performed in two ways. Pre- increment/post-increment and pre-decrement/post-decrement. If you declare the variable I =5 then if you want to perform I++ operation in C first the value 5 will be stored into I after assigning only it will be incremented as 6. So in next statement only I value should go to value 6.
2. The next one is precedence of the operator’s. You must consider the priority of all the operators. The precedence has the order like
High priority: * / %
Low priority: + -
Another one to understand is
For this entire operator it should give the high priority for which one statement has the ( ) operator like (operators + operands). If one statement has this entire operator in the same line then it should perform operation from left to right.
Like this only printf function should perform, so for your program you got the result like 6, 6, and 5.
Int i=5;
I++ ( I=5) // I value assigned the next line will be incremented
I (I=6) // incremented value only assigned
I—( I=6) // after assigning the value 6 then only it’ll decrement the value to 5
ans:6,6,5;
![]() |
Similar Threads
- CODING QUESTION: What is the MOST importanting thing to do when...... (Computer Science)
- Sams excercises (C)
- why following output ??? (C)
Other Threads in the C Forum
- Previous Thread: little formatting question
- Next Thread: data validation problem
| Thread Tools | Search this Thread |
#include adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop framework frequency getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix probleminc process program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socket socketprograming stack standard string strings systemcall testautomation threads turboc unix user variable voidmain() wab windows.h






