what's mean by definition and declaration in c?

how u call this:
int i;

Recommended Answers

All 3 Replies

Of a function or a simple value such as i.
Well you can declare i by

int i;

You can then store any value in it by

scanf("%d", i);

This would accept almost any integer value(with some limits).
For a function you must first declare a prototype by the following:

int example(int x, int y)

You can then define what the function has to do by introducing curly braces({}) after the prototype and including the code block in it.
At last you can call the example function by the following:

example(var1, var2);

Further more read this http://hubpages.com/hub/What-is-Function-How-to-Declare-it-in-C-Part-1.
If this solved your problem mark this thread as solved. Else reply to it again.

@shikhin :
you forgot the & in scanf :)

it should be:

scanf("%d", &i);

@shankarz:
I hope the explanation given by shikhin is clear to you. I'll just summarize what he said:

Declaring a variable means to tell the compiler about the type of the variable :

int myVariable;

Initializing a variable means to assign it a value :

myVariable = 420;

In ANSI C (correct me if I'm wrong) I believe you can combine the two steps into one:

int myVariable = 420;

Definition is a term used mostly with functions and as was explained by shikhin it deals with implementing the function by writing or "defining" its body within curly braces :

int myFunction (){
    //code written here, defines the function
}

Hope it helps.

Generally speaking a definition actually creates something while a declaration just tells the compiler it exists.

This can be done for both variables and functions. A variable definition can also include a value for initialisation.

So for variables

// Assume all at file scope

int i;      // This is a definition, you'd expect to find it in a .c file

int ii = 5; // This is a definition with initialisation , you'd expect to find it in a .c file

extern int i; // This is a declaration, you'd expect to find it in a .h file

extern int iii = 5; // This is a definition with initialisation, you'd expect to find it in a .c file

The definitions on lines 3 and 5 cause the compiler to reserve memory for the variable, line 5 also provides an initialisation value. Either of these can also be written with the static keyword to limit the variables scope to the current file, otherwise the variable implicitly has global scope.

Line 7 is a declaration, it says only that the variable exists, the compiler will not reserve memory, the linker will have to resolve this symbol.

However line 9 is a definition again because it contains an initialiser, the compiler will reserve memory. Additionally it is explicitly declared as have global scope.

For a function

int Myfunction(int g);  // This is a declaration

// This is a function definition
int Myfunction(int g)
{
    return (g + 375) % 1000;
}

The function declaration on line 1 tells the compiler the function exists, normally appearing at the top of a .c file or in a header. However does not cause the compiler to generate any output to the object file.

The is a function definition on line 4 causes the compiler to output the object code for this function to the object file presumably for use in the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.