views:

109

answers:

4

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");
  }
}
A: 

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

Martin Beckett
A: 
David Leonard
Only in a mode not conforming to strict C99; C99 requires an explicit type on all functions and variables.
Jonathan Leffler
A: 

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.

jamesdlin
A: 

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.

Jonathan Leffler