Difference between Declaration , initialisation and definition.

This is a discussion on Difference between Declaration , initialisation and definition. within the C Programming forums, part of the General Programming Boards category; Hi,
Can any one give small explanation about Declaration , initialization and definition in c language?
Because i have little ...

Because your 2.c file has code outside of a function... you are directly including a C file (which is major bad juju) and you aren't using headers. Also try to pick something other than lazy arsed names for things... really single letters... not good.

Hit those textbooks again and look up how to structure multi-file projects.

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:

Code:

x = 5;

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.

No it means that you should not be directly including .c files into one another... you risk creating loops the compiler will not know how to resolve.

This is why you write header files .h to be included into your .c files... Headers only contain declarations, extern variables and constants and only as needed by the page you are including them into... The stdio.h file used in almost every console program is a good example... go ahead open it in a text editor (don't change anything!) and you will see there is no code in the file, just declarations, externs, constants and typedefs...

I've seen your messages across several of the forums here and I do hope you don't mind a little advice...

You need to learn C or C++ by an act of deliberate study. You are not going to absorb it by blundering around trying this, that and the other thing... You need to sit down with the textbooks and take on a deliberate course of learning before it's going to make any sense to you at all. That is: start on page 1 of the book, read the text, compile the examples, play with the code until you understand it, review as needed... then move to page 2... and so on until you finish the book.

At that point you should be able to write small projects -- balance your chequebook, do a home inventory, simple games, etc. -- without too much trouble. After that it's a matter of practice practice practice. C is a very simple language; programming, on the other hand, is an extremely complex task.

Seriously... I see this a lot on these forums... People think that because they read chapter 1 of C for Dummies, they're suddenly qualified to rewrite Windows7... Believe me... it just doesn't work that way.

More accurately, this "specific value" is the initial value. MK27's example is actually of assignment, not initialization, though it may also be used to provide what is effectively an initial value (overwriting an existing "garbage" value).

Originally Posted by MK27

Definition applies to types and functions.

It also applies to variables. The definition of a variable is a declaration of the variable that causes storage to be reserved for it.

Looking at the source can be useful and informative, of course. If your C library is open source (such as on linux), you can get a copy of whatever .c file it's in if you want. Those will be in the source package for glibc, and via:

If your library is not open source, you are out of luck, but a gander at glibc might satisfy your curiosity if you are interested seeing in a very widely used, standards compliant code definition of printf(). The source from ftp above is in a g'zipped tarball, so you need the *nix commands "tar" and "gzip" to open it. I'm sure there are crossplatform versions around (eg, from minGW).