Search code examples
coperator-precedence

Is it possible to pass two arguments in c?


I have written a C program where I declared a function reverse(int i). When I compile and run the program, it runs fine despite passing two arguments like this reverse((i++, i)). Why doesn't this cause a syntax error? reverse expects one argument.

  #include <stdio.h>
    void reverse(int i);
    int main()
    { 
            reverse(1); 

    }
    void reverse(int i)
    {
            if (i > 5)
                    return ;
            printf("%d ", i); 
            return reverse((i++, i));
    }

Solution

  • (i++, i) seems to execute i++, then evaluate to i, the last operand to ,. You can see that here:

    // Notice the ( , )
    int i = (puts("Inside\n"), 2); // Prints "Inside"
    
    printf("%d\n", i); // Prints 2
    

    It didn't cause an error because you only passed one argument. That one argument though was a sequence of effects that evaluated to i.