Difference between Declaration and Defination of variable

Please anyone can help me
Difference between Declaration and Definition of variable?

Last edited on
One of them you declare it eg mention it is there int x; and the other you define it eg say what it is x = 10;
The declaration would look more like:
 
extern int x;
That means there's an x somewhere and it's an int.

The definition would look like:
 
int x;
That means create storage the size of an int and call that memory location x.

This allows your to write code like:
header.h
1
2
extern int x;  // declare variable
int func(void);// declare function 


worker.c
1
2
3
4
5
6
#include "header.h"

int func(void)  // define function
{
    return 2*x; // use x from declaration, the linker resolves the declared variable to a defined variable
}


main.c
1
2
3
4
5
6
7
8
#include "header.h"

int x;  // define variable

int main()
{
    return func();// use func from declaration, the linker resolves the declared function to a defined function
}

Last edited on
Thanks every one for reply.
Please check this out -

Declaration:- not get memory to variable.
e.g. extern int x;

Definition:- get space in memory and store default value.
e.g. int x;

Initialization:- get some(proger given) value for that variable.
e.g. int x =10;
That's mostly right, except global and static variables are initialised to zero.

It's not obvious that that should be the case. But it happens to be easy for the linker to arrange. These variables with program long lifetime get placed into a segment that's zeroed in one go.
thank you kbw.
Topic archived. No new replies allowed.