Q:

What is the difference between declaration and definition of a variable?

-3

What is the difference between declaration and definition of a variable?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

Declaration of a variable in C

A variable declaration only provides sureness to the compiler at the compile time that variable exists with the given type and name, so that compiler proceeds for further compilation without needing all detail of this variable. In C language, when we declare a variable, then we only give the information to the compiler, but there is no memory reserve for it. It is only a reference, through which we only assure the compiler that this variable may be defined within the function or outside of the function.

Note: We can declare a variable multiple times but defined only once.
eg,

extern int data;
extern int foo(int, int);
int fun(int, char); // extern can be omitted for function declarations
Definition of a variable in c

The definition is action to allocate storage to the variable. In other words, we can say that variable definition is the way to say the compiler where and how much to create the storage for the variable generally definition and declaration occur at the same time but not almost.

eg,

int data;
int foo(int, int) { }

Note: When you define a variable then there is no need to declare it but vice versa is not applicable.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the difference between global and static g... >>