I am reading the book "Programming in C" and found in Chapter 10 an example like this:
#include <stdio.h>
void test (int *int_pointer)
{
*int_pointer = 100;
}
int main (void)
{
void test (int *int_pointer);
int i = 50, *p = &i;
printf ("Before the call to test i = %i\n", i);
test (p);
printf ("After the call to test i = %i\n", i);
return 0;
}
I understand the example, but I don't understand the line void test (int *int_pointer);
inside of main
. Why do I define the signature of test
again? Is that idiomatic C?
We can declare a function inside a function, but it's not a nested function. Because nested functions definitions can not access local variables of the surrounding blocks, they can access only global variables of the containing module.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
The reason modern compilers give warnings on an attempt to call a function before seeing a declaration is that a declaration allows the compiler to check if arguments are of the expected type.
Function declarations are important because a calling convention (part of the ABI of a platform) can define different places for arguments and return values based on the types a function returns and accepts as arguments.
Declaration: the function's name, return type, and parameters (if any) For code optimization, it is recommended to separate the declaration and the definition of the function. You will often see C programs that have function declaration above main (), and function definition below main ().
If a function is defined above the main function, there is no need of a separate declaration of function. However, if the function is defined below the main function, it is a good programming practice to declare the functions being used above the main.
Function declarations are hoisted but function expressions are not. It’s easy to understand with an example: The above does not throw an error, but this would: It might seem like function declarations, with their powerful hoisting properties, are going to edge out function expressions for usefulness.
However, note that each declaration must be consistent with its definition with regard to the number of parameters, their types and the return type. A function declaration provides valuable information (function name, number and type of parameters and return type) to the compiler.
It's definitely not idiomatic C, despite being fully valid (multiple declarations are okay, multiple definitions are not). It's unnecessary, so the code will still work perfectly without it.
If at all, perhaps the author meant to do
void test (int *int_pointer); int main (void) { ... }
in case the function definition was put after main ()
.
void test (int *int_pointer);
is just a declaration (or prototype) of function test
. No need of this declaration in main
because you already have function definition before main
.
If the definition of test
were after main
then it would be worth of putting its declaration there to let the compiler know about the return type, number of arguments and arguments types of test
before calling it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With