开发者

How can c let a function declaration with any parameter type?

开发者 https://www.devze.com 2022-12-24 05:02 出处:网络
I forgot to write void parameter but it works the i put void it gives error it lets this: print(int size,int table[size][size]){

I forgot to write void parameter but it works the i put void it gives error it lets this:

print(int size,int table[size][size]){
  int i,j;
  printf("-------TABLE-------\n");
  for(i = 0;i<size;i++){
    for(j = 0;j<size;j++){
       if(table[i][j]==EMPTY)
       开发者_高级运维 printf(". ");
       else
        printf("* ");
    }
    printf("\n");
  }
}

it says"previos implicit declaration was here " (means the call in main)

void print(int size,int table[size][size]){
  int i,j;
  printf("-------TABLE-------\n");
  for(i = 0;i<size;i++){
    for(j = 0;j<size;j++){
       if(table[i][j]==EMPTY)
        printf(". ");
       else
        printf("* ");
    }
    printf("\n");
  }
}


If you declare a function

foo(int x) {
}

the compiler will infer the return-type to be int, as if you had written

int foo(int x) {
}

But, really, that's the least of your problems.

Reference: §1.3.1 of The C Library Reference Guide by E. Huss


It's a historical artifact. This happens for backwards compatibility with old C code (from before C was standardized). Functions without explicit return types are assumed to return int, and if no explicit parameters are specified, it's assumed to take some unspecified number of arguments.


In C, in contrast to C++, and assuming you are not using a strict C99 conformance mode in your compiler, then when the compiler comes across something that looks like a function call but the identifier has not previously been declared, then the compiler assumes that it is the name of a function returning an integer.

int main(void)
{
     something_functional(1, 2);
}

Without a prototype, the compiler in C90 or relaxed C99 mode will assume that 'something_functional()' is indeed a function returning an integer.

If you subsequently write:

something_functional(double d, char *s)
{
    ...
}

The compiler will assume that the return type of 'something_functional()' is an int and will let the code compile (despite the horrendous mismatch between the actual argument types in the call and in the function definition).

If you subsequently write:

void something_functional(douebl d, char *s)
{
    ...
}

the compiler will, quite correctly, complain that you've told it different things about the function and that makes it erroneous.

There are several ways to fix the problem:

  1. Move the function definition ahead of the function call. This is what Pascal required, and leads to the main() function appearing at the bottom of the file.
  2. Place a prototype for the function ahead of its use in the file:

    static void something_functional(double d, char *s);

I take the view that if the function is not going to be called from outside the source file, it should be static.

Note that C99 in a strict conforming mode does not allow you to declare functions implicitly. C++ never has either.

There's another interesting feature of pre-C99. You could also write:

static c;
static function();

some_function(a, b)
{
}

This defines an integer variable c, a function called function() that returns an int, and a function called some_function() that takes two int arguments and returns an int.

This is not allowed in strict C99 code - and was not particularly good style in C90 code, but was allowed for backwards compatibility with pre-standard C.


without void eg, function() says the function declaration might have any parameters.
function(void) says that it has precisely none.

0

精彩评论

暂无评论...
验证码 换一张
取 消