Function Compiles Which Arguments First?

This is a discussion on Function Compiles Which Arguments First? within the C Programming forums, part of the General Programming Boards category; A teacher posed a question in class earlier asking if a function that three arguments, which is read first, second, ...

  1. #1
    Exodus
    Guest

    Function Compiles Which Arguments First?

    A teacher posed a question in class earlier asking if a function that three arguments, which is read first, second, and third (assuming that there are three of them). I've read that it depends on the compiler, but can't seem to get any farther than that. Does anyone have any more input?

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    say again...?

  3. #3
    Exodus
    Guest

    Once Again

    Ok let try to explain myself once agian. . .

    function(int x, int y, int z)
    {

    . . .

    }

    Which argument is recognized first, second, and third? Apparently it depends on the compiler, but my teacher wishes for us to write a program/experiment proving this one way or another. Any suggestions?

  4. #4
    Unregistered
    Guest
    #include<stdio.h>

    void show_it(int x,int y,int z)
    {
    printf("the id x is at addr: %8x\n",&x);
    printf("the id y is at addr: %8x\n",&y);
    printf("the id z is at addr: %8x\n",&z);
    }

    int main(void)
    {
    int x,y,z;

    x=y=z=0;
    show_it(x,y,z);
    }

    A run of it produces:

    the id x is at addr: bffff9a0
    the id y is at addr: bffff9a4
    the id z is at addr: bffff9a8

    so z is recognized first.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    There are two aspects to this question

    1. arguments are pushed onto the stack in right to left order, so
    show_it(x,y,z);
    pushes the arguments z, y, x
    This is basically what the previous program tells you

    2. arguments can be evaluated in any 'compiler' specific order, and this is something you can never find out (or rely on, even if you manage to figure it out).
    show_it(x++,x++,x++);
    is what you might try to figure this out, but any such programs are by definition undefined (read: wrong).

    When a compiler evaluates the parameters and when it finally decides to push those arguments are different things.

    Mmmm
    Code:
    #include <stdio.h>
    
    int foo ( int x ) {
        printf( "%d\n", x );
        return x;
    }
    
    void show_it(int x,int y,int z) {
        printf("the id x is at addr: %p\n",&x);
        printf("the id y is at addr: %p\n",&y);
        printf("the id z is at addr: %p\n",&z);
    }
    
    int main ( ) {
        show_it( foo(1), foo(2), foo(3) );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Sending multiple arguments to function..
    By Zzaacchh in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2008, 03:20 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 01:15 PM
  4. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 02:26 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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