A definition is always a declaration. The difference is that a definition also gives whatever you declare some value.
In your example, by the way, it is only a redeclaration error:
f(int a, /* Defines a */
int b)
{
int a; /* Declares a - error! */
a=20; /* initializes a */
return a;
}
You probably meant to do this:
f(int a, /* Defines a */
int b)
{
int a = 20; /* Declares and defines a - error! */
return a;
}
But in this case, most compilers will throw a "redeclaration" error too. For example, GCC throws the following error:
Error: 'a' redeclared as a different kind of symbol
That is because a
is originally defined as a parameter, which is different from a variable definition inside the function's scope. As the compiler sees that you're re-declaring something that is of a different "breed" than your new declaration, it can't care less if your illegal declaration is a definition or not, because it regards "definition" differently in terms of function parameters and function local variables.
However, if you do this:
int c = 20;
int c = 20;
GCC, for example, throws a redefinition error, because both c
-s are the function's local variables.