0

This is my main function and what I am passing.

int main(void){
   struct can elC[7];    // Create an array of stucts 

   Initialize(elC); // initializes the array

   return 0;
}

int Initialize(struct can elC[7]){

}

In C don't we have to declare the function before main or something? If so how would it look? My code runs fine with the declaration of

int Initialize();

But don't I need something like

int Initialize(struct can elc[7]);

2 Answers2

0
/* Declaration of Structure */
struct can {
   /* Structure members */
};

/* Function Prototype - ANY OF THE THREE */
int Initialize();
int Initialize(struct can []);
int Initialize(struct can var[]);

int main (void)
{
   struct can elC[7];   // Create an array of stucts 
   Initialize(elC);     // Call to function - Initializes the array
   return 0;
}

int Initialize(struct can elC[7])
{
        //Do something;
        return 0;
}

What happens if you don't declare prototype

The below will work just fine.

$ gcc filename.c   

When combined with -Wall option for warnings, it will throw a warning.

$ gcc filename.c -Wall
In function ‘main’:
Warning: implicit declaration of function ‘Initialize’ [-Wimplicit-function-declaration]

So its always better to declare the prototype before main like

int Initialize(struct can []);   

or

int Initialize(struct can var[]);

Below is valid too, means that you can pass any number of arguments. See here.

int Initialize();   
Community
  • 1
  • 1
Saurabh Meshram
  • 8,106
  • 3
  • 23
  • 32
  • Only two of the three declarations are prototyped; the other is just a function declaration without introducing a prototype. You've also not mentioned that both C99 (the old standard) and C11 (the current standard) require functions to be declared before they are used — only C89 (the very old standard) allows the use of functions without prior declarations. – Jonathan Leffler Sep 04 '14 at 04:23
  • I suppose you are talking about `int Initialize();` But they have function name so it should conflict right ? – Saurabh Meshram Sep 04 '14 at 04:26
  • It is mainly a question of nomenclature. All three are function declarations; only the last two are prototyped declarations. The other simply announces "there is a function `Initialize()` that returns an `int` and could take any fixed number of arguments". It cannot be a variadic function like `printf()`; those must have a prototype in scope, even in C89. – Jonathan Leffler Sep 04 '14 at 04:28
  • `int Initialize();` meaning it could take any number of arguments, but wouldn't that conflict with `int Initialize(struct can []);` since they have same function name, but taking different arguments ? – Saurabh Meshram Sep 04 '14 at 04:34
  • I was kinda assuming that you'd only actually have one of the three declarations; there is no point in having all three. If you have the non-prototype declaration and one of the prototype declarations, the prototype is in force. Since the two prototype declarations are equivalent, either or both can be present, but there's no merit in the repetition. – Jonathan Leffler Sep 04 '14 at 04:35
  • Yes in my answer what I meant is `ANY OF THE THREE` prototype not all or even both. So as I understand having only `int Initialize();` is fine and matches to the function definition ie. `int Initialize(struct can elC[7]) { return 0;}` as per old standards (c89) ? – Saurabh Meshram Sep 04 '14 at 04:44
  • `int Initialize();` is a valid function declaration, but it is ***NOT*** a prototype. Prototypes have a list of argument types, or `ReturnType function_name(void)` to indicate that there are no arguments. – Jonathan Leffler Sep 04 '14 at 05:07
  • @JonathanLeffler clarifying your earlier comment, calling a variadic function without a prototype in scope must compile successfully, however it causes undefined behaviour if program execution reaches that function call. (Caveat: arguably, if the compiler can deduce that program flow always reaches that function call, then it can cause time-travelling undefined behaviour and reject the program!) – M.M Sep 04 '14 at 06:16
0

In C89 you do not have to declare a function before calling it; however if you do call an undeclared function, it is as if you declared the function as:

int funcname();

The empty parentheses do not mean that the function takes no parameters; it just means that our declaration is not a prototype. The number and types of the parameters are unknown at this stage (but it isn't a function taking a variable argument list like printf() because even in C89, those must have a prototype in scope when they are used).

If the actual function returns something other than int, or you call it with the wrong type or number of parameters, or if any of the parameters have a different type after the default argument promotions are applied, then calling the function causes undefined behaviour.

Your code doesn't fall foul of any of those limitations, so your code is correct in C89. However it's considered good style to use function prototypes anyway as it enables the compiler to detect all of those conditions and report an error.

In C99 you do have to declare the function before calling it. An appropriate prototype would be:

int Initialize(struct can *elC);

Also I would advise to use C99 or C11 if your compiler supports them. Regarding the use of [] in a function parameter list, see here.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365