Declaration and initialisation of variables

Apr 27, 2010 at 3:39am
Hi all,

What is difference between,
int a=10;

and

int a;
a=10;

I have checked executing a sample code in assembly mode but i didn't find any difference. please comment on this.
Apr 27, 2010 at 4:59am
for basic types like int, there's probably not going to be any difference.

For complex types like classes Foo a = bar; calls a ctor that takes 'bar' as a parameter.

Whereas Foo a; a = bar; calls the default ctor, then makes a seperate call to the assignment operator.
Apr 27, 2010 at 5:11am



they both ultimately do two things for your program

1. it declares the data type that you may want to use in this case int.
2. the value 10 becomes assigned to your invented identifier in this case its a.


They are two options with the same outcome. The c like variable initialization ( int a=10;) is more often used when your declaring local variables.
Last edited on Apr 27, 2010 at 5:15am
Apr 28, 2010 at 2:44am
+1 Disch
Topic archived. No new replies allowed.