简体   繁体   中英

Function header/declaration

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]);
/* 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();   

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM