Originally Posted by
nkrao123@gmail.
Can any one give small explanation about Declaration , initialization and definition in c language?
A declaration declares and names a variable, type, or function, eg:
Code:
int x; // variable declaration
struct whatever; // type declaration
int somefunc(int); // function declaration, aka prototype.
Note that unless int x is global, that declaration leaves it uninitialized, meaning it could have any value (if it is global, it is initialized to 0). Initialization is the assignment of a specific value, eg:
Definition applies to types and functions. Just as variables may be declared and initialized at the same time (eg, "int x = 5"), types and functions may be declared and defined at the same time:
Code:
struct whatever {
int x;
};
int somefunc(int x) {
return x*2;
}
Those look the same if the type or function has already been declared. You may declare a type or function multiple times (hopefully, the same way), but you may only have one definition. The reason for separate, potentially multiple declarations (which are optional) is to do with compiling multiple files into a single executable.
Originally Posted by
nkrao123@gmail.
hi,
"you aren't using headers"-- this mean if i include #include"1.c" in 2.c earlier program would work?
Nope. You put your definitions in a .c file and declarations in a .h (header) file. Then you include the .h file.
eg.h
Code:
struct whatever;
int somefunc(int);
eg.c
Code:
struct whatever {
int x;
};
int somefunc(int x) {
return x*2;
}