pushing of arguments into the stack?

This is a discussion on pushing of arguments into the stack? within the C Programming forums, part of the General Programming Boards category; Code: int main() { int l =8; printf("%d\n%d\n",l,l++); return 0; } Output received A 8 // But why..... B 8 ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    pushing of arguments into the stack?

    Code:
    int main()
    {
     int l =8;
    
     printf("%d\n%d\n",l,l++);
    
     return 0;
    }
    Output received
    A8 // But why.....
    B8

    1. how are the arguments pushed to the stack ? isn't this right to left?

    if its right to left then output A should be 9 right?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    It's not necessary that the arguments will always be passed from right to left. It's compiler specific. So the result is actually undefined(vary from compiler to compiler).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by BEN10 View Post
    It's not necessary that the arguments will always be passed from right to left. It's compiler specific. So the result is actually undefined(vary from compiler to compiler).
    With an compiler that pushes the argument right to left, shouldn't the output be 9 and 8.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    The result of l++ is 8.
    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

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    My question is why the operation l++ is not executed?

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sanddune008 View Post
    With an compiler that pushes the argument right to left, shouldn't the output be 9 and 8.
    Yeah. For such a compiler l++ will be 8 and then l incremented to 9.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sanddune008 View Post
    My question is why the operation l++ is not executed?
    What are you talking about? I compiled your code and it produces the correct output(as it is passing arguments right to left).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> With an compiler that pushes the argument right to left, shouldn't the output be 9 and 8.

    That's why is called 'undefined behavior'. Consider this bit of code:

    Code:
    foo( bar++ );
    The compiler could break this down into:

    Code:
    push bar
    call foo
    increment bar
    - or perhaps -

    Code:
    push bar
    increment bar
    call foo
    Naturally then, had the code been:

    Code:
    foo( bar, bar++, bar );
    The compiler may well generate:

    Code:
    push bar
    push bar
    push bar
    call foo
    increment bar
    So in other words, it's all very unpredictable.



    ITSA
    Socket Library!

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by BEN10 View Post
    What are you talking about? I compiled your code and it produces the correct output(as it is passing arguments right to left).
    nope....i getting output as 8 and 8

    besides i am using Visual studio 6.0

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    thanks....Sebastiani

  11. #11
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Compile with warnings.

    In this case, the only thing that is guaranteed is that l will be incremented before entering the body of printf(), but parameter values the function is called with are undefined.

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    It is not advisable to increment or decrement the same variable in a single statement. Your title of this was a bit off key as well.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> In this case, the only thing that is guaranteed is that l will be incremented before entering the body of printf()

    No. There is no guarantee here whatsoever.



    ITSA
    Socket Library!

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Sebastiani
    No. There is no guarantee here whatsoever.
    I think msh is correct: there is a sequence point before control enters the function, so it should be guaranteed "that l will be incremented before entering the body of printf()".

    EDIT:
    Ah, but I see that that is a moot point when in combination with the fact that there is undefined behaviour - "parameter values the function is called with are undefined" also means that l can have any value.
    Last edited by laserlight; 07-08-2009 at 08:03 AM.
    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

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    >> I think msh is correct: there is a sequence point before control enters the function, so it should be guaranteed "that l will be incremented before entering the body of printf()".

    Ok, I see what you mean now. The variable is first copied onto the stack, then incremented, and finally the function executes. Thanks for clarifying that.



    ITSA
    Socket Library!

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

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