What is the difference between definition and declaration?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between definition and declaration??

in c or c++

13th Mar 2017, 7:04 PM
Mrinal Kanti Ghosh
Mrinal Kanti Ghosh - avatar
7 Answers
+ 7
int x; You are declaring the variable x = 5; You are defining it's value int x = 5; You are declaring the variable and assigning it a value.
13th Mar 2017, 8:13 PM
J.G.
J.G. - avatar
+ 6
int x; // declaration int y = 5; // definition I think that declaration is declaring a variable (x is of type int) And that definition is defining a variable (x is 5)
13th Mar 2017, 7:13 PM
J.G.
J.G. - avatar
+ 5
@Mrinal No! It allocates memory when you create a variable (declare it as something). So when you do int x; it allocates memory for an integer named x. This is why you can use pointers when a variable doesn't have a value assigned. Technically, though, it does have a value when declared. When you define it you are just changing its value. It has a default value of null. So: int x; int* ptr = &x; cout <<ptr<<endl <<x; There has to be something there for the pointer to point to.
13th Mar 2017, 8:54 PM
J.G.
J.G. - avatar
+ 1
Have you had a look at abstract classes? Then you'll know.
13th Mar 2017, 8:04 PM
1of3
1of3 - avatar
+ 1
I want to say in declaration doesn't occupy any memory.... the moment we define it by assigning a value to it , occupies memory... am I r8????? @J.G
13th Mar 2017, 8:17 PM
Mrinal Kanti Ghosh
Mrinal Kanti Ghosh - avatar
+ 1
J.G I will suggest u Google about difference between declaration and definition... u can find there the concept of allocation of memory.. only by definition memory is allocated...😎😎😎😎😎
14th Mar 2017, 11:48 AM
Mrinal Kanti Ghosh
Mrinal Kanti Ghosh - avatar
0
yahh it's ok from Syntex but what is the difference between int x; x = 5; and int x=5; @j.g
13th Mar 2017, 7:21 PM
Mrinal Kanti Ghosh
Mrinal Kanti Ghosh - avatar