8

I have understood the difference between declaration and definition And I was practicing some question when I hit the doubt, the below code asked me to list out the error in the snippet.

f(int a,int b)
{
     int a;
     a=20;
     return a; 
}

Why does this gives re-declaration error of a? Shouldn't it give multiple definition of a because in:

  • f(int a,int b) — here a is defined right?
  • and in the function body, int a is defined again?

So why not multiple definition error?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 7
    Definitions are also declarations. The multiple declaration error is diagnosed before it checks for multiple definitions – M.M Feb 16 '15 at 05:05
  • The parameter list declares `f(int a...)`, you then declare `int a` in the body -- redeclaration... – David C. Rankin Feb 16 '15 at 05:05
  • 2
    @DavidC.Rankin I think he's asking why the error message says "re-declaration" instead of "multiple definition" – M.M Feb 16 '15 at 05:05
  • 1
    Rohit you should include the compiler name and version and the exact error message in the question – M.M Feb 16 '15 at 05:06
  • Gotcha, thanks Matt. – David C. Rankin Feb 16 '15 at 05:06
  • 1
    I liked the explanation of Matt thanks, though I knew that definition is a superset of declaration, Its a like hitting the first mistake while compilation and reporting it rather than checking the further mistake –  Feb 16 '15 at 05:11
  • 1
    Well matt, after reading your comment, I thought, I get the answer to my question but isn't it that we can declare the variable as many time as we want ? So Mints answer below makes much more sense that the declaration of the function parameters differs from the declaration of the local variable, So the compiler gives this error. –  Feb 16 '15 at 05:45

2 Answers2

9

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.

  • 2
    Well I like your answer, Here is what I understood correct me if I am wrong, So you are saying that the declaration of the parameter of the function is different from the declaration of the local variable so we get this error just like in this case extern int a; and extern char a ; has the different types of declaration –  Feb 16 '15 at 05:52
  • @RohitSaluja: a parameter of a function is *always* a definition. It is declared in the function header and initialized by calling the function with some arguments, i.e. when the function is executed, all parameters will be initialized, hence they will be *defined*. When you try to re-define a parameter inside a function, it is *sort of* alike to a confilcting-type error like in your example, yeah, but in this case, it's because a parameter and a local variable are *different kinds of symbols* in that they're defined in a different fashion. –  Feb 16 '15 at 05:56
  • Why are you relating definition to the initialization, I guess they are both the different things, Initialization is just providing the variable with some value, nothing more than that :/ –  Feb 16 '15 at 05:59
  • @RohitSaluja: if we're speaking in terms of local variables, definition = declaration + initialization, i.e. `int c = 20` declares `c` and initializes it to the value `20`, thus *defining it* as a variable with the value 20, and `int c` simply declares it. Stuff's a bit different in function-scope, you see =) –  Feb 16 '15 at 06:00
  • @RohitSaluja: yes, one could probably argue that this reasoning (definition = declaration + initialization) is incorrect terminology by the standard (as per the standard, a definition of an object is anything that reserves memory for the object), but this is how most compilers differentiate between local variable definition and declaration. –  Feb 16 '15 at 06:08
  • Well in your previous comment that int c merely declares the variable c is not correct, It defines c as we can take the address of the variable and initialize it to the pointer even thought the variable is not holding any value. AFAIK I ve never came across such terminology that definition =declaration + initialization , I guess I cannot state it in much more simple way that may be I got the error because the declaration of the local variable is different from the declaration of the variable in the parameter of the function –  Feb 16 '15 at 06:18
  • @RohitSaluja: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70991/discussion-between-mints97-and-rohit-saluja). –  Feb 16 '15 at 06:19
1

When you declare a variable in your code,a block of memory will be allocated with that variable name depending on the data type used in your declaration.

But when you try to redeclare the same variable the processor tries to allocate the memory which is already allocated with the same name.so as the processor face ambiguity while trying to access the memory block with that variable name the compiler do not allow that instruction, hence multiple declarations will not be allowed and you will get a error in GCC compiler telling,

line 3 col 10    [Error]redeclaration of 'int a'
line 1 col 7     [Error]'int a' previously declared here

in your code

  f(int a,int b)    //first declaration of 'a'
  {
       int a;       //redeclaration of 'a', whose memory is already allocated
       a=20;
       return a; 
  }

on the memory layout two blocks cannot have same identity(variable name), hence the compiler throws a redeclaration error as multiple declarations are not possible, when variable 'a' is redeclared.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    I guess you are confused between declaration and definition. It is the definition which allocates the memory not the declaration. For more info on declaration and definition follows this [link](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration). –  Feb 16 '15 at 09:03