You are here:

C/c programming basics

Advertisement


Question
Please tell me a deatiled difference between DECLARATION and DEFINITION IN C(WITH EXAMPLES)

THANK YOU

Answer
Declaration just tells the type of the variable (in case of functions, it tells the type of parameters and the return type).
Definitions allocates space also (In case of functions, the function body is the definition).
Usually for a variable, the declaration and definition are same.
So, for the statement:
int i;
It is both a declaration and also a definitions.
But, if you use it in another file also:
extern int i;
This is just a declaration.

Similarly for a function:
int fun(char *str);
is the function declaration, also called as function prototype.
and:
int fun(char *str)
{
   if (str != NULL) return (strlen(str));
   else return 0;
}
is the function definition.

Hope it is clear now.

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.