6

Possible Duplicate:
What is the difference between a definition and a declaration?

I am trying to thoroughly understand "defining" and "declaring" in C.

I believe x here is defined, since external variables are automatically initialized to 0, and something that's declared and initialized is defined. Is that accurate?

int x;
main() {}

According to one x in this case is a definition, but why? It is not being initialized...

int print_hello()
{
  int x;
}
Community
  • 1
  • 1
user257412
  • 706
  • 1
  • 9
  • 21
  • Read [this](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632) answer. – Prasoon Saurav Dec 10 '10 at 17:45

3 Answers3

3

Declaring is telling the compiler that there's a variable out there that looks like this.

Defining is telling the compiler that this is a variable.

One refers to the existence of a thing, the other is the thing.

In your example, the scope is what makes the difference. Declarations are made in a file scope, but in a block scope it is not possible to declare anything; therefore, the second example is a definition; because, there is nothing left to do with the int x;.

That makes the first example (in the file scope) a declaration that some int x; exists. To covert it from a declaration, you need to specify that a value is assigned to it, forcing the memory allocation. Like so: int x = 0;

C and C++ are very scope sensitive when it is analyzing constructs.

Edwin Buck
  • 64,804
  • 7
  • 90
  • 127
  • thanks but I understood that much :) Edit: so , its defined because thats the only alternative ?? I think I'll just have to mugup this . – user257412 Dec 10 '10 at 17:41
  • Sorry, I hit submit too soon so I had to edit it to add the part that dealt with your actual question. – Edwin Buck Dec 10 '10 at 17:43
  • but his first example is also a definition, no? – lijie Dec 10 '10 at 17:44
  • 1
    Actually the first example is not a definition, it is only a declaration. To convert it to a definition, you need to assign a value to it. The rules differ slightly between C and C++. That's why it is far better to enforce the use of the `extern` keyword. – Edwin Buck Dec 10 '10 at 17:44
  • the first is definitely a definition.. i think – user257412 Dec 10 '10 at 17:52
  • Tentative definitions are different from regular definitions. Edwin is pretty much accurate, except that a tentative definition is not a simple forward declaration, it is a definition, but there can be more than one of them that refer to the same object. Be careful! – Carl Norum Dec 10 '10 at 17:53
1

"Define" does not mean "initialized." It means something is created, rather than just referenced.

A definition allocates but does not necessarily initialize memory. This can lead to fun debugging.

Andy Thomas
  • 78,842
  • 10
  • 93
  • 142
  • so even if a variable hasn't been initialized it can be defined ? but i read that definition = declaration + initialization, here - http://ubuntuforums.org/showthread.php?t=1501482&page=1 – user257412 Dec 10 '10 at 17:51
  • From c-faq: "definition - n. 1. A declaration of a variable or function which allocates and optionally initializes (in the case of a variable) or provides the function body (in the case of a function)." -- http://c-faq.com/sx1/index.html#definition – Andy Thomas Dec 10 '10 at 18:12
  • should have read that... ok so the answer is yes. even if a variable hasn't been initialized it can be defined. The point is that for definition memory has to be allocated ? – user257412 Dec 10 '10 at 18:18
  • For variables, yes, a definition allocates memory. There are other, um, definitions for other constructs. Common among all is that there can be a single definition, but multiple declarations. – Andy Thomas Dec 10 '10 at 19:25
1

Declaration introduces a name in a TU. Definition instantiates/allocates storage for that name.

int x; //definition,also a declaration. Every definition is a declaration.
int main(){}
rMan
  • 71
  • 3