26

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?

6
  • By declaring a variable means you are telling the compiler to reserve a space for the data type of that variable. Commented Oct 11, 2013 at 20:37
  • 8
    @haccks that's a definition, not a declaration. Commented Oct 11, 2013 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. Commented Oct 11, 2013 at 20:46
  • But highlighted in accepted answer. Commented Oct 11, 2013 at 20:47
  • 1
    @haccks: that does not say that is THE only way to declare it ;) Commented Oct 11, 2013 at 20:49

3 Answers 3

24

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.

Sign up to request clarification or add additional context in comments.

12 Comments

How int x; be a declaration? o.O.
@haccks I'm not sure I get the question, could you rephrase it?
Actually I am totally confused now. :(
@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 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)
|
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.                                     .

Comments

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.

Comments