25

I was reading some info on externs. Now the author started mentioning variable declaration and definition. By declaration he referred to case when: if a variable is declared the space for it is not allocated. Now this brought me to confusion, because I think MOST of the times when I use variables in C, I am actually both defining and declaring them right? i.e.,

int x; // definition + declaration(at least the space gets allocated for it)

I think then that only cases in C when you declare the variable but not define it is when you use:

extern int x; // only declaration, no space allocated

did I get it right?

  • By declaring a variable means you are telling the compiler to reserve a space for the data type of that variable. – haccks Oct 11 '13 at 20:37
  • 8
    @haccks that's a definition, not a declaration. – ouah Oct 11 '13 at 20:38
  • @haacks: its not really a duplicate, I looked at that one, although they are very similar. But in my case I wanted to *make sure* the ONLY case when we are dealing with declaration is with extern. That is not highlighted in that question. –  Oct 11 '13 at 20:46
  • But highlighted in accepted [answer](http://stackoverflow.com/a/5151535/2455888). – haccks Oct 11 '13 at 20:47
  • 1
    @haccks: that does not say that is THE only way to declare it ;) –  Oct 11 '13 at 20:49
  • The answers so far do not cover whether a struct member declaration is a declaration or a definition – M.M Jul 24 '17 at 09:41

3 Answers3

23

Basically, yes you are right.

extern int x;  // declares x, without defining it

extern int x = 42;  // not frequent, declares AND defines it

int x;  // at block scope, declares and defines x

int x = 42;  // at file scope, declares and defines x

int x;  // at file scope, declares and "tentatively" defines x

As written in C Standard, a declaration specifies the interpretation and attributes of a set of identifiers and a definition for an object, causes storage to be reserved for that object. Also a definition of an identifier is a declaration for that identifier.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • How `int x;` be a declaration? o.O. – haccks Oct 11 '13 at 20:46
  • 1
    @haccks I'm not sure I get the question, could you rephrase it? – ouah Oct 11 '13 at 20:48
  • Actually I am totally confused now. :( – haccks Oct 11 '13 at 20:50
  • I am accepting this, because I had similar idea about this, though 'tentative' definition is not that clear to me, but so be it. –  Oct 11 '13 at 20:51
  • @ouah; Read [this](http://wiki.answers.com/Q/Difference_between_the_definition_and_declaration_of_a_variable_in_). – haccks Oct 11 '13 at 21:03
  • @haccks maybe could you quote here some important statements from this link? – ouah Oct 11 '13 at 21:07
  • `Declaration can come only once and definition can come many times in the program. Let's take an example, Line 1: #include Line 2: Line 3: void main() Line 4: { Line 5: int x; //declaration Line 6: Line 7: x=10; //definition Line 8: }` – haccks Oct 11 '13 at 21:21
  • Here is the [link](http://wiki.answers.com/Q/Difference_between_the_definition_and_declaration_of_a_variable_in_c) – haccks Oct 11 '13 at 21:21
  • 7
    @hackks you should not trust some low quality text from the internet. The quote is wrong. You cannot define an object multiple times in C and `x = 10;` is a statement which performs an assignment and not a definition. – ouah Oct 11 '13 at 21:36
  • 1
    @ouah you can in fact have multiple definitions in C if it's a file-scope object and only one is non-tentative (e.g. `int x; int x = 10;` at file scope is OK) – M.M Dec 03 '18 at 01:40
  • @M.M your example is valid C, as are multiple tentative definitions valid in C. But your example does not show a multiple definition. The first declaration is a tentative definition and it does not constitute a definition in the sense that it does not reserve storage for that object. The tentative definition become a definition of the object only in absence of the external definition. – ouah Dec 04 '18 at 19:59
  • @ouah tentative definitions are still definitions (the word "tentative" is an adjective qualifying "definition"). The rule is that you cannot have multiple external definitions. A tentative definition does cause storage to be reserved for the object, bearing in mind that multiple tentative definitions refer to the same object. – M.M Dec 04 '18 at 20:50
0

This is how I view it putting together bits and pieces I found on the internet. My view could be askew.
Some basic examples.

int x;
// The type specifer is int
// Declarator x(identifier) defines an object of the type int
// Declares and defines

int x = 9;
// Inatializer = 9 provides the initial value
// Inatializes 

C11 standard 6.7 states A definition of an identifier is a declaration for that identifier that:

— for an object, causes storage to be reserved for that object;

— for a function, includes the function body;

int main() // Declares. Main does not have a body and no storage is reserved

int main(){ return 0; } 
  // Declares and defines. Declarator main defines                  
  // an object of the type int only if the body is included.

The below example

int test(); Will not compile. undefined reference to main
int main(){} Will compile and output memory address.

// int test();
int main(void)   
{
    // printf("test %p \n", &test); will not compile 
    printf("main %p \n",&main);
    int (*ptr)() = main;

    printf("main %p \n",&main);

 return 0;
}

extern int a;  // Declares only.
extern int main(); //Declares only.

extern int a = 9;  // Declares and defines.
extern int main(){}; //Declares and  defines.                                     .
damx
  • 1
  • 2
0

During declaration, a memory location is reserved by the name of that variable, but during definition memory space is also allocated to that variable.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103