4

Possible Duplicate:
Why does gcc allow arguments to be passed to a function defined to be with no arguments?

the code:

#include <stdio.h>
#include <stdlib.h>

void test_print()
{
    printf("test print\n");
}

int main()
{
    test_print(1,2);
    return 0;
}

although the caller of test_print in main has different amount of arguments with the defination of this function, the code can work very well, but if change it into c++ version, there occurs a compile error "too many arguments to funciton ....". why C allows argument mismatch call of function, when can we use this way of calling? and why it's forbidened in c++.

System ubuntu 11.10
compiler: gcc 4.6.1

Community
  • 1
  • 1
nzomkxia
  • 1,219
  • 4
  • 16
  • 35
  • In C99 this invokes undefined behavior. See: C99 6.5.2.2/6 _If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined._ – cpx Dec 26 '12 at 06:48
  • http://stackoverflow.com/questions/13950642/why-does-a-function-prototype-with-no-parameters-compared-to-the-actual-functio/13950800#13950800 – Krishnabhadra Dec 26 '12 at 06:49

4 Answers4

4

In c empty () on an function means the function can take any number of arguments.
If you want to specify that function does not take any arguments you need to:

void test_print(void) 

While, in C++ empty () on an function means it does not take any arguments. Note that this is significant in C++ because in C++ you can overload a function based on number of arguments it can take.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

Because according to the C standard;

int foo(void); //Is the only way a function won't allow any parameters

Quoting the standard:

10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

Leaving the parenthesis blank means non or any number of arguments.

Fingolfin
  • 5,363
  • 6
  • 45
  • 66
2

When you write:

void test_print() { ... }

you have not provided a prototype for the function, so the compiler is not supposed to compare calls with the argument list. To provide a prototype, you have to write an explicit void:

void test_print(void) { ... }

Or provide a separate prototype declaration:

void test_print(void);

But it is best to make the function definition match the prototype declaration, so always write the void. And yes, this is one of the areas where C++ differs from C. You can't use a function in C without a prototype in scope and C++ is able to treat an empty argument list as an empty argument list. In C99 or later, you're supposed to have a prototype in scope, but it isn't usually enforced by the compiler unless you add more stringent options (-Wmissing-prototypes -Wstrict-rprototypes -Wold-style-definition -Wold-style-declaration are possible GCC options). But the backwards compatibility requirements with pre-standard C meant that C89 could not enforce the 'empty parentheses means no arguments' rule without breaking a lot of previously valid C code, which would have prevented the standard from being acceptable.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

It is a good programming discipline to ensure that you find the compiler option that ensures that all functions are declared with a full prototype before being used, and to ensure that you use the compiler option all the time, and heed its warnings

Avio
  • 29
  • 2