Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function call in C [duplicate]

Tags:

c

function

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

like image 997
nzomkxia Avatar asked Jan 14 '23 16:01

nzomkxia


1 Answers

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.

like image 140
Alok Save Avatar answered Jan 23 '23 05:01

Alok Save