Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to the unspecified arguments in function()? [duplicate]

Tags:

c

function

I have been reading the difference between function() and function(void) in C, and I came to know that

an empty parameter list in a function declaration indicates that the function takes an unspecified number of parameters

So I ran this code:

#include <stdio.h>

void fun();

int main(void)
{
    fun(12, 13.22, 1234567890987654321, "wow", 'c');
}

void fun()
{
    printf("What happened to those arguments?");
}

I don't understand why C allows this. All the posts I've read so far related to this say things like it's bad practice to use it, it is obsolescent, etc. Also from the above code, I think the type of those arguments is also unspecified. So I just want to know the reason behind "unspecified arguments of unspecified type":

  • What can be done with those arguments?

  • Is it possible to access those arguments within the function?

like image 213
Ardent Coder Avatar asked May 25 '20 20:05

Ardent Coder


People also ask

Which type of arguments are used in a function when number of arguments not known in advance?

Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls with an arbitrary number of arguments. In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument.

When function is called by passing a copy of the value of the arguments is called?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in.

What could be the purpose of the arguments inside a function?

In mathematics, an argument of a function is a value provided to obtain the function's result. It is also called an independent variable. , is called a unary function.


Video Answer


1 Answers

The reason for supporting the notation is historical. Before the first C standard (C89/C90), you couldn't use prototypes in C; prototypes were one of the biggest and most important features of Standard C. All function declarations, therefore, were written the 'empty parentheses' style (when they were written at all; most functions that returned int were not declared at all). The type void was also added in C89/C90, though some compilers supported it before the standard was finalized.

Because it was crucial to the success of C89/C90 that existing code should mostly continue to work, the empty parentheses style had to be allowed by the standard. So, your code might have been written in pre-standard C as:

#include <stdio.h>

int fun();  /* This declaration would probably have been absent */

int main(void)
{
    fun(12, 13.22, 1234567, "wow", 'c');
    return 0;   /* This was required until C99 to give reliable exit status */
}

fun(i, d, l, s, c)      /* No return type - implicitly returns int */
long l;                 /* Defined out of sequence - bad style, but legal */
char c;                 /* Passed as int; converted to char in function */
char *s;                /* Should define all pointer arguments */
double d;               /* No definition of i; it was an int by default */
{
    printf("This is what happened to those arguments:\n");
    printf("i = %d\n", i);
    printf("d = %f\n", d);
    printf("l = %ld\n", l);
    printf("s = [%s]\n", s);
    printf("c = %c\n", c);
    /* No return statement - don't use the value from the function */
}

For the curious: you could omit the char *s; line in the function definition, and it still compiled and produced the same output. It was a bad idea to try that, though. You could replace the line int fun(); with static fun(); and the code compiles cleanly when no diagnostics are requested.

You get no warnings even now if you compile this file (old31.c) with GCC 9.3.0 using:

$ gcc -std=c90 -o old31 old31.c
$

Your example as written is skirting around the backwards compatibility provisions. Using void means it was new code (it would not have been valid in many pre-standard C compilers because it used void). And new code should not exploit the backwards-compatibility provisions without a good reason. That was true in 1991 as well as in the current millennium (but in 1991, there were a lot more good reasons to exploit the backwards-compatibility provisions). Good pre-standard code usually listed all parameters in the order they were used. Omitted definitions and out of sequence definitions were not entirely satisfactory.

You asked:

  • What can be done with those arguments?

In the code in the question, nothing can be done with the arguments. The caller pushes the values onto the stack, and pops them off when the function returns. The called function is unaware of their existence and can do nothing with them.

  • Is it possible to access those arguments within the function?

No — not using any standard mechanism.

like image 166
Jonathan Leffler Avatar answered Sep 22 '22 23:09

Jonathan Leffler