i++ query

This is a discussion on i++ query within the C Programming forums, part of the General Programming Boards category; hi guys,i tried this thing on my turbo c compiler...actually my frnd asked me for theoutput for the code... this ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515

    i++ query

    hi guys,i tried this thing on my turbo c compiler...actually my frnd asked me for theoutput for the code...

    this is the code....

    Code:
     #include<stdio.h>
     #include<conio.h>
      void main()
     {
       int i=10;
       i=(i++)/(i++);
       printf("%d",i);
       getch();
     }
    i got the output as 3
    apparently...the compiler is calculating i/i first and the incrementing i 2 times....but........if i directly say

    Code:
    printf("%d",i++/i++);
    it does not give me the same result
    actually wat i want to ask is this....
    when a division is output directly in printf,as in the case that i have give,it calculates the numerator first and then the denominator...bt,if i calculate for the division inside the main() func,and then assign the value to a variable

    say something like

    case 1:
    Code:
    b=i++/i++;
    i get the output as 1,

    whereas,if i say

    case 2:
    Code:
     printf("%d",i++/i++);
    i get the o/p as 0;

    in the first case i think it is doing wat is to be done in the denominator first and then goin on to the numerator...

    bt in the second one .... it does wat is in the numarator first...and then goes on to the denominator...


    y does the compiler have double standards while calculating in different conditions....

    any help is appreciated...

    p.s.i also tried it out in the borland compiler...and got the same results...

    ciao

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    1. Dont be a void main()er

    2. You're probably working with code whose output is undefined by the standards.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    thanx salem....
    bt wat i want to ask is bout the part where i the numerator and the denominator are evaluated diferntly...is that compiler dependent or is it the same for all compilers??

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    bt wat i want to ask is bout the part where i the numerator and the denominator are evaluated diferntly...is that compiler dependent or is it the same for all compilers??
    The problem, as I see it, would be that you simply cant use the expression (i++/i++) without getting an undefined result.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Undefined means undefined. When you do something undefined like i = (i++)/(i++) you shouldn't be surprised if it caused your computer to format your harddrive or even initiate a nuclear attack. Avoid undefined code.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Quote Originally Posted by laserlight
    The problem, as I see it, would be that you simply cant use the expression (i++/i++) without getting an undefined result.

    i dont get wat u mean by an undefined result....wat i am gettin to is that y does my compiler calculate the division in a different way when i directly output it in the printf() func....u see...under normal circumstances....suppose we say.....

    Code:
     a=b+c+d;
    then the compiler calcs..(c+d) then adds the res to b;
    so the compiler goes frm right to left doesnt it??
    then y go frm left to right in the printf ()??

    i hope i havnt made any mistake in assuming the right to left format of the compiler

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    i dont get wat u mean by an undefined result....wat i am gettin to is that y does my compiler calculate the division in a different way when i directly output it in the printf() func
    In a sense, an undefined result means that the compiler is free to set any result.

    So i++/i++ could be 0, 1, or -1 etc regardless of the value of i.

    so the compiler goes frm right to left doesnt it??
    then y go frm left to right in the printf ()??
    Have you visited the website that Salem linked to?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Quote Originally Posted by laserlight
    In a sense, an undefined result means that the compiler is free to set any result.

    So i++/i++ could be 0, 1, or -1 etc regardless of the value of i.


    Have you visited the website that Salem linked to?
    i did visit the site....but i dont tink that there is anything relating to this aspect.....correct me if m wrong...i cud have overlooked something....i understood wat u say bout the undefined thing....
    the answer i get to the i=i++/i++ thing is 3....and i tried it on atleast 3 compilers and got the same result.....also,the left and right ting seems to have gotten me confused...please help

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    the answer i get to the i=i++/i++ thing is 3....and i tried it on atleast 3 compilers and got the same result
    Yet a compiler could be written that gives the answer as 2, and you cant complain about that since the result was undefined anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If you have multiple side effects on the same variable in the same expression, then ALL bets are off.

    > the answer i get to the i=i++/i++ thing is 3....and i tried it on atleast 3 compilers and got the same result
    Which proves nothing about the other n-3 compilers in the world

    If you make the expression complicated enough, and start turning on things like optimisation, then you're pretty sure to start finding some differences.
    Side_effect: (++x + y*x + y*x) != (y*x + ++x + y*x) ?

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    An undefined result is like trying to divide a number by 0. There is no answer. Just don't do it. The result you get, or the different results you get with different compilers/platforms/etc., can't be explained because the result is undefined.
    If you understand what you're doing, you're not learning anything.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to be having trouble with the word. Maybe this link will help.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Quote Originally Posted by quzah
    You seem to be having trouble with the word. Maybe this link will help.


    Quzah.
    hmm...i visited the site....its not that i am not understanding the meaning of the word UNDEFINED ..... bt the thing is that.....how can u classify as to wat i am askin as undefined??

    wat i am askin is that in the standard c compiler...the turbo one....if a calculation is to be done , then the righthand most side is calculated then the thing to the left of that and then to the left of that and so on....
    bt when i set up a calculation directly in the printf () func,it is being calculated in a different manner...

    for eg..
    if i say
    i=10;
    then i say
    b=i++/i++

    first it chks out wat is in the denominator....its i++
    so it puts the value of i in the denominator and then increments it.
    then it chks out the numerator...its i++ again
    so now it puts the new value of i that is 11 in the numerator and then increments i to 12.

    but it hapns in the opp manner when the ting is passed directly to printf() or for matter any other statement like if() or while()...wat i am askin is that is there any specific reason y the compiler does this ??

    i know that m being a pain...bt plz help.
    ciao

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    how can u classify as to wat i am askin as undefined??
    When the language's standard classifies such an expression as such.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple query language
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 04:29 PM
  2. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  3. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  4. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 09:44 AM
  5. WBEM Query too slow
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 05:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21